#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll ans;
stack<pair<int,int> > s;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
int x,p=1;
cin>>x;
while(s.size()&&s.top().first<=x){
ans+=s.top().second;
if(s.top().first==x){
p+=s.top().second;
}
s.pop();
}
if(s.size()) ans+=s.top().second;
s.push(make_pair(x,p));
}
cout<<ans;
return 0;
}
这个代码是 28pts。
然后改
ans+=s.top().second;
为
ans++;
就AC了。
所以为什么是 ans++ 而不是 ans+=s.top().second?