题解
2026-08-06 13:49:18
发布于:浙江
8阅读
0回复
0点赞
正解是单调栈,但是ACGO数据有点水,不用好像也能过,但既然是刷题,那还是用单调栈好
#include<cstdio>
#include<stack>
typedef long long ll;
const int N=5e5+5;
ll n,h[N],ans;
int main(){
scanf("%lld",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&h[i]);
std::stack<std::pair<ll,ll>>st;
ll ans=0;
for(int i=1;i<=n;i++){
std::pair<ll,ll>p(h[i],1);
while(st.size() && st.top().first<=h[i]){
ans+=st.top().second;
if(h[i]==st.top().first)p.second+=st.top().second;
st.pop();
}
if(st.size())ans++;
st.push(p);
}
printf("%lld",ans);
return 0;
}
全部评论 1
智齿
1周前 来自 浙江
0







有帮助,赞一个