标签:lan space recommend main bsp algo const har div
Input
Output
Sample Input
7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0
Sample Output
8 4000
Hint
单调栈的最入门题目。O(n)求一下l,r两个数组即可。
1 #include<cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 #include <stack> 6 typedef long long ll; 7 typedef unsigned long long ull; 8 #define pii pair <int, int> 9 #define pll pair <ll, ll> 10 #define mp make_pair 11 using namespace std; 12 const int MAX=1e5+5; 13 int n; 14 int a[MAX],l[MAX],r[MAX]; 15 stack <int> mono; 16 ll an; 17 int main() 18 { 19 while(scanf("%d",&n)&&n) 20 { 21 an=0; 22 for(int i=1;i<=n;i++) 23 scanf("%d",&a[i]); 24 while(!mono.empty()) 25 mono.pop(); 26 for(int i=1;i<=n;i++) 27 { 28 while(!mono.empty()&&a[mono.top()]>=a[i]) 29 mono.pop(); 30 if(mono.empty()) 31 l[i]=0; 32 else 33 l[i]=mono.top(); 34 mono.push(i); 35 } 36 while(!mono.empty()) 37 mono.pop(); 38 for(int i=n;i>=1;i--) 39 { 40 while(!mono.empty()&&a[mono.top()]>=a[i]) 41 mono.pop(); 42 if(mono.empty()) 43 r[i]=n+1; 44 else 45 r[i]=mono.top(); 46 mono.push(i); 47 } 48 for(int i=1;i<=n;i++) 49 an=max(an,(ll)((ll)r[i]-(ll)l[i]-1LL)*(ll)a[i]); 50 printf("%lld\n",an); 51 } 52 return 0; 53 }
(单调栈)poj-2559 Largest Rectangle in a Histogram
标签:lan space recommend main bsp algo const har div
原文地址:http://www.cnblogs.com/quintessence/p/6618848.html