QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#573805 | #9313. Make Max | jiangdong | WA | 0ms | 3568kb | C++14 | 2.0kb | 2024-09-18 20:01:19 | 2024-09-18 20:01:19 |
Judging History
answer
//单调栈
#include<bits/stdc++.h>
using namespace std;
const int md=2e5+5;
int n,t,cnt,k,flag;
stack<int>sz,num;
void solve(){
cin>>t;
while(t--){
cin>>n;
cnt=0;
while(sz.size())sz.pop();
while(num.size())num.pop();
cin>>k;
sz.push(1),num.push(k);
for(int i=1;i<n;i++){
flag=0;
cin>>k;
if(k==num.top())sz.top()++;
else if(k<num.top())num.push(k),sz.push(1);
else{
while(num.size()>1){
int kk=num.top();num.pop();
int op=sz.top();sz.pop();
if(num.top()==k){//与当前栈顶相同,则把先前小于它的数同化。同时更新总次数与栈顶元素个数
cnt+=op;
sz.top()+=op+1;
flag=1;
break;
}else if(num.top()<k){//小于栈顶元素,推入栈顶,将先前小于它的数同化,更新栈顶元素个数与总次数
num.push(k);
cnt+=op;
sz.push(op+1);
flag=1;
break;
}else{//大于当前栈顶元素 则将推出的上一个栈顶元素与现栈顶元素同化,并更新总次数与栈顶元素个数
sz.top()+=op;
cnt+=op;
}
}if(flag==1)continue;//k已被同化或推入栈内
else{num.top()=k;
cnt+=sz.top();
sz.top()+1;
}
}
}while(sz.size()>1){//形成单调栈 总次数与栈顶元素个数随之更新即可
int pp=sz.top();
sz.pop(),num.pop();
cnt+=pp;
sz.top()+=pp;
}cout<<cnt<<'\n';
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3568kb
input:
4 2 1 2 2 2 2 7 1 1 1 2 2 2 2 3 1 2 3
output:
1 0 3 2
result:
wrong answer 4th numbers differ - expected: '3', found: '2'