标签:show none col 假设 递增 lse stack 合并 png
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 6 int n, h[100005], w[100005], s[100005], top = 0; 7 //h --> height w --> width s --> stack 8 inline bool input(){ 9 scanf("%d", &n); 10 if(!n) return false; 11 for(int i = 1; i <= n; i++) 12 scanf("%d", &h[i]); 13 h[n + 1] = 0;//最后用来清空栈 14 return true; 15 } 16 17 inline long long work(){ 18 long long ans = 0; 19 for(int i = 1; i <= n + 1; i++){ 20 if(h[i] > s[top]){ 21 s[++top] = h[i]; 22 w[top] = 1;//宽度为1 23 }//满足单调 24 else{ 25 int widthsum = 0;//宽度和 26 while(s[top] > h[i]){ 27 widthsum += w[top]; 28 ans = max(ans, (long long) widthsum * s[top]);//注意高是s[top] 29 top--; 30 } 31 s[++top] = h[i];//此时s[top]已经小于h[i],满足单调 32 w[top] = widthsum + 1;//合并 33 } 34 } 35 return ans; 36 } 37 38 int main(){ 39 while(input()){ 40 printf("%lld\n", work()); 41 top = 0; 42 memset(s, 0, sizeof(s)); 43 memset(h, 0, sizeof(h)); 44 memset(w, 0, sizeof(w)); 45 } 46 return 0; 47 }
POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈
标签:show none col 假设 递增 lse stack 合并 png
原文地址:https://www.cnblogs.com/New-ljx/p/11225837.html