【题解】hdu1506 Largest Rectangle in a Histogram

题目

Largest Rectangle in a Histogram

思路

7 2 1 4 5 1 3 3

$Code$

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<stack>
#include<algorithm>
#define max(a,b) a>b?a:b
#define MAXN 100001

typedef long long ll;

ll x=0;bool f=0;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=!f;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
T=f?-x:x;
}

inline void write(ll x) {
if(x<0) putchar('-'),write(-x);
else {
if(x/10) write(x/10);
putchar(x%10+'0');
}
}

ll n;
int l[MAXN],r[MAXN];
struct node {
ll w,num;
}a[MAXN];
std::stack<node> sss;

signed main() {
while(1) {
ll maxx=0;
for(int i=1;i<=n;++i) {
}
for(int i=1;i<=n;++i) {
if(sss.empty()) {l[i]=0;sss.push(a[i]);continue;}
node x=sss.top();
while(x.w>=a[i].w) {
sss.pop();
if(sss.empty()) break;
x=sss.top();
}
if(sss.empty()) l[i]=0;
else l[i]=sss.top().num;
sss.push(a[i]);
}
while(!sss.empty()) sss.pop();
for(int i=n;i>=1;--i) {
if(sss.empty()) {r[i]=n+1;sss.push(a[i]);continue;}
node x=sss.top();
while(x.w>=a[i].w) {
sss.pop();
if(sss.empty()) break;
x=sss.top();
}
if(sss.empty()) r[i]=n+1;
else r[i]=sss.top().num;
sss.push(a[i]);
}
for(int i=1;i<=n;++i) {
ll qwq=a[i].w*1ll*(r[i]-l[i]-1);
maxx=max(maxx,qwq);
}
while(!sss.empty()) sss.pop();
write(maxx);puts("");
}
return 0;
}
posted @ 2019-11-11 22:08  yu__xuan  阅读(...)  评论(...编辑  收藏