提交时间:2024-04-07 19:09:09

运行 ID: 142389

#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; }