https://blog.csdn.net/zuzhiang/article/details/78135142
http://poj.org/problem?id=2559
my code_1 solution 1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
ll read()
{
char ch=' ';
ll f=1;ll x=0;
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';ch=getchar();
}
return x*f;
}
const ll N=100100;
ll a[N];
stack <ll> s;
int main()
{
ll n;
ll i,j;
while(scanf("%lld",&n)!=EOF)
{
if(n==0) break;
while(!s.empty()) s.pop();
for(i=1;i<=n;i++)
{
a[i]=read();
}
a[n+1]=-1;
ll ans=0;
ll top,tmp;
for(i=1;i<=n+1;i++)
{
if(s.empty()||a[i]>=a[s.top()])
{
s.push(i);
}
else
{
while(!s.empty()&&a[i]<a[s.top()])
{
top=s.top();
tmp=(i-top)*a[top];
ans=max(ans,tmp);
s.pop();
}
s.push(top);
a[top]=a[i];
}
}
cout<<ans<<endl;
}
return 0;
}
my code solution 2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
ll read()
{
char ch=' ';
ll f=1;ll x=0;
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';ch=getchar();
}
return x*f;
}
const ll N=100100;
ll l[N],r[N];
ll a[N];
stack <ll> s;
void init()
{
while(!s.empty())
{
s.pop();
}
}
int main()
{
ll n;
ll i,j;
while(scanf("%lld",&n)!=EOF)
{
if(n==0) break;
init();
for(i=1;i<=n;i++)
{
a[i]=read();
}
a[n+1]=-1;
a[0]=-1;
for(i=1;i<=n+1;i++)
{
if(s.empty()||a[i]>=a[s.top()])
{
s.push(i);
}
else
{
while(!s.empty()&&a[i]<a[s.top()])
{
r[s.top()]=i;
s.pop();
}
s.push(i);
}
}
init();
for(i=n;i>=0;i--)
{
if(s.empty()||a[i]>=a[s.top()])
{
s.push(i);
}
else
{
while(!s.empty()&&a[i]<a[s.top()])
{
l[s.top()]=i;
s.pop();
}
s.push(i);
}
}
ll ans=0;
for(i=1;i<=n;i++)
{
ans=max((r[i]-1-l[i]-1+1)*a[i],ans);
}
cout<<ans<<endl;
}
return 0;
}