NYOJ 228最大长方形(二)

 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define max(a,b) a>b?a:b
 6 int stack[100010]={-2},len[100010];
 7 int main()
 8 {
 9     int t,i,w,h,top;
10     long long result;
11     while(scanf("%d",&t)!=EOF)
12     {
13         if(t==0)continue;
14         top = -1;
15         result = 0;
16         for(i=0; i<=t; ++i)  //要多输入一个数来和最后一个数比较
17         {
18             if(i<t)    scanf("%d",&h);
19             else    h = -1;
20             if(stack[top]<h || top < 0)
21             {
22                 stack[++top] = h;
23                 len[top] = 1;
24             }
25             else
26             {
27                 w = 0;
28                 while(stack[top] >= h && top >= 0)
29                 {
30                     result = max(result, (long long)stack[top]*(w + len[top]));
31                     w += len[top--];
32                 }
33                 if(h > 0)
34                 {
35                     stack[++top] = h;
36                     len[top] = w + 1;    //这是很关键的一步,要仔细的品味一番其中的精妙之处
37                 }
38             }
39         }
40         printf("%lld\n",result);
41     }
42     return 0;
43 }
posted @ 2013-04-07 19:52  YaLing  阅读(236)  评论(1编辑  收藏  举报