QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#573065 | #9313. Make Max | Harold0895 | WA | 29ms | 5356kb | C++14 | 1.6kb | 2024-09-18 17:13:51 | 2024-09-18 17:13:52 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mxn=2e5+5,inf=0x3f3f3f3f;
#define int long long
int a[mxn],n;
struct ST{
int pre;//当前元素的前一个元素的num
int l,r;//当前元素的左右边界
int num;//当前元素的值
};
void solve(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
stack<ST>s;//栈中的一个元素是一个集合体,含有多个数
s.push({inf,0,0,inf});//压入一个无穷大入栈
a[++n]=inf;
int p=1;
int ans=0;
while(p<=n){//从左到右走一遍O(n)
ST & temp=s.top();//栈顶
if(a[p]<temp.num){//当前元素比栈顶小,还不能直接比较,需等后续元素入栈
s.push({temp.num,p,p,a[p]});
p++;
}
else if(a[p]==temp.num){//当前元素和栈顶相等,直接改栈顶的区间
temp.r=p;
p++;
}
//以下情况为(a[p]>temp.num)
else if(temp.pre>a[p]){//三个元素从左到右为ABC,即B<C<A,栈顶元素temp.num(B)和a[p] (C)合并
ans+=temp.r-temp.l+1;
temp={temp.pre,temp.l,p,a[p]};//修改栈顶
p++;
}
else if(temp.pre<=a[p]){//三个元素从左到右为ABC,即B<A<C,栈顶元素temp.num(B)和temp.pre (A)合并直到temp.pre>temp.num
ans+=temp.r-temp.l+1;
s.pop();
s.top().r=p;
}
}
cout<<ans-n+1<<'\n';
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T;
cin>>T;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3680kb
input:
4 2 1 2 2 2 2 7 1 1 1 2 2 2 2 3 1 2 3
output:
1 0 3 3
result:
ok 4 number(s): "1 0 3 3"
Test #2:
score: -100
Wrong Answer
time: 29ms
memory: 5356kb
input:
2 198018 875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...
output:
4183864 4225726
result:
wrong answer 1st numbers differ - expected: '4084978', found: '4183864'