题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657
单调栈水题。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int const xn=50005; int n,h[xn],v[xn],sta[xn],top,s[xn]; int rd() { int ret=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();} while(ch>='0'&&ch<='9')ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar(); return f?ret:-ret; } int main() { n=rd(); for(int i=1;i<=n;i++) { h[i]=rd(),v[i]=rd(); while(top&&h[sta[top]]<=h[i])top--; if(top)s[sta[top]]+=v[i]; sta[++top]=i; } top=0; for(int i=n;i;i--) { while(top&&h[sta[top]]<=h[i])top--; if(top)s[sta[top]]+=v[i]; sta[++top]=i; } int mx=0; for(int i=1;i<=n;i++)mx=max(mx,s[i]); printf("%d ",mx); return 0; }