关于这题的一个小问题
查看原帖
关于这题的一个小问题
464732
luqyou楼主2023/1/9 15:33
#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;
}

这个代码是 28pts28pts

然后改

ans+=s.top().second;

ans++;

就AC了。

所以为什么是 ans++ 而不是 ans+=s.top().second

2023/1/9 15:33
加载中...