刘嘉柚 • 1个月前
sb雨水
#include<bits/stdc++.h>
using namespace std;
vector<int>height;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int x;cin>>x;
height.push_back(x);
}
if(height.size()==0)cout<<0;
else{
stack<int> s;
int ans=0;
int t;
for(int i=0;i<height.size();i++)
{
while(s.size()&&height[s.top()]<height[i])
{
t=s.top();
s.pop();
if(!s.size())break;
int l=s.top();
int r=i;
int w=r-l-1;
ans+=(min(height[i],height[l])-height[t])*w;
}
s.push(i);
}
cout<<ans;
}
return 0;
}
评论: