题目链接:http://poj.org/problem?id=2559
解法:单调栈
这个题是 https://blog.youkuaiyun.com/qq_41646772/article/details/96701780 的简化版,维护一个单调递增的栈,栈内存下标。这题没什么好说的,看代码就能明白
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
typedef long long LL;
int N;
LL h[100005],ans;
stack<int>sta;
int main()
{
while(~scanf("%d",&N)&&N)
{
while(!sta.empty())sta.pop();
for(int i=0;i<N;i++)scanf("%lld",&h[i]);
h[N]=-1;
ans=0;
for(int i=0;i<=N;i++)
{
if(sta.empty()||h[i]>=h[sta.top()])sta.push(i);
else
{
int top;
while(!sta.empty()&&h[i]<h[sta.top()])
{
top=sta.top();
sta.pop();
LL x=i-top,y=h[top];
ans=max(ans,x*y);
}
sta.push(top);
h[top]=h[i];
}
}
printf("%lld\n",ans);
}
return 0;
}