标签:type port body rect ram ... log using div
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22171 | Accepted: 7173 |
Description
Input
Output
Sample Input
7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0
Sample Output
8 4000
Hint
Source
#include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <queue> #include <stack> #include <vector> #include <iostream> #include "algorithm" using namespace std; typedef long long LL; const int MAX=100005; LL n; struct Node{ LL x; LL y; }sta[MAX]; LL h[MAX],ans; int main(){ freopen ("histogram.in","r",stdin); freopen ("histogram.out","w",stdout); int i,j; LL zt; while (scanf("%lld",&n),n!=0){ ans=0; for (i=1;i<=n;i++){ scanf("%lld",h+i); } int top; top=1; sta[0].x=0,sta[0].y=0; sta[top].x=1; sta[top].y=h[1]; for (i=2;i<=n;i++){ if (h[i]>=sta[top].y){ sta[++top].y=h[i]; sta[top].x=i; } else{ while (top>0 && h[i]<sta[top].y){ zt=((i-1)-sta[top-1].x)*sta[top].y; ans=max(ans,zt); top--; } sta[++top].x=i; sta[top].y=h[i]; } } for (i=1;i<=top;i++){ ans=max(sta[i].y*(sta[top].x-sta[i-1].x),ans); } printf("%lld\n",ans); } return 0; }
POJ-2559 Largest Rectangle in a Histogram(单调栈)
标签:type port body rect ram ... log using div
原文地址:http://www.cnblogs.com/keximeiruguo/p/7226599.html