80分 WA on #21~23 求助
查看原帖
80分 WA on #21~23 求助
1033739
aCBHa楼主2025/8/3 15:19
#include<bits/stdc++.h>
using namespace std;
int n,h;
long long s;
struct node
{
	int h,cnt;
};
stack<node> st;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		cin>>h;
		if(!st.empty()&&st.top().h==h)
		{
			st.top().cnt++;
			continue;
		}
		while(!st.empty()&&st.top().h<h)
		{
			node u=st.top();
			st.pop();
			s+=u.cnt*(u.cnt-1)/2+u.cnt;
			if(!st.empty())
				s+=u.cnt;
		}
		if(!st.empty()&&st.top().h==h)
			st.top().cnt++;
		else
			st.push(node{h,1});
	}
	while(!st.empty())
	{
		node u=st.top();
		st.pop();
		s+=u.cnt*(u.cnt-1)/2;
		if(!st.empty())
			s+=u.cnt;
	}
	cout<<s;
	return 0;
}

2025/8/3 15:19
加载中...