提交时间:2024-04-07 21:42:02

运行 ID: 142411

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