傻逼zzy...
poj2796
注意一下最后答案可能是0,所以res的初始值要赋为-1
#include <iostream>
#include <cstdio>
using namespace std;
int n,top;
long long res;
int ans;
long long s[100005], a[100005];
int L[100005], R[100005];
struct node
{
long long x;
int pos;
node(long long a = 0, int b = 0):x(a), pos(b){}
}st[100005];
int main()
{
scanf("%d", &n);
s[0] = 0;
for (int i=1;i<=n;i++)
{
scanf("%lld", &a[i]);
s[i] = s[i-1] + a[i];
L[i] = 0;R[i] = n+1;
}
top = 0;
for (int i=1;i<=n;i++)
{
while (top > 0 && st[top].x > a[i])
{
R[st[top].pos] = i;
top--;
}
st[++top] = node(a[i], i);
}
top = 0;
for (int i=n;i>=1;i--)
{
while (top > 0 && st[top].x > a[i])
{
L[st[top].pos] = i;
top--;
}
st[++top] = node(a[i], i);
}
ans = 0;
res = -1;
for (int i=1;i<=n;i++)
{
if (res < a[i] * (s[R[i]-1] - s[L[i]]))
{
res = a[i] * (s[R[i]-1] - s[L[i]]);
ans = i;
}
}
printf("%lld\n", res);
printf("%d %d\n", L[ans]+1, R[ans]-1);
}
poj2559
//最大矩形面积
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
#define N 100005
LL ans,a[N];
int n,top;
int L[N], R[N];
struct node
{
LL x;
int pos;
node (LL a = 0, int b = 0):x(a), pos(b){}
}st[N];
int main()
{
while (scanf("%d", &n) && n)
{
for (int i=1;i<=n;i++)
scanf("%lld", &a[i]);
top = 0;
for (int i=1;i<=n;i++)
{
while (top > 0 && st[top].x > a[i])
{
R[st[top].pos] = i;
top--;
}
st[++top] = node(a[i], i);
}
while (top > 0)
R[st[top].pos] = n+1, top--;
top = 0;
for (int i=n;i>=1;i--)
{
while (top > 0 && st[top].x > a[i])
{
L[st[top].pos] = i;
top--;
}
st[++top] = node(a[i], i);
}
while (top > 0)
L[st[top].pos] = 0, top--;
ans = 0;
for (int i=1;i<=n;i++)
ans = max(ans, a[i] * (R[i] - L[i] - 1));
printf("%lld\n", ans);
}
}