1. 程式人生 > 其它 >求最大矩形面積2(棧)

求最大矩形面積2(棧)

#include<iostream>
#include<stack>
using namespace std;
const int maxn = 105;
int n,data[maxn],t[maxn];
stack<int>s;
int main(){
    int ans;
    while(cin>>n){
        for(int i=1;i<=n;i++){
            cin>>data[i];
        }
        ans = 0;
        for(int i=1;i<=n;i++){
            
if(s.empty()||s.top()<=data[i]){ s.push(data[i]); } else{ int count = 0,now_ans = 0; while(!s.empty()&&s.top()>data[i]){ count++; now_ans = s.top()*count; if
(now_ans>ans){ ans = now_ans; } s.pop(); } while(count>=0){ s.push(data[i]); count--; } } } int count = 0,p = s.size(),len = p;
while(!s.empty()){ t[p--] = s.top();s.pop(); } for(int i=1;i<=len;i++){ ans = max(ans,t[i]*(len-i+1)); } cout<<ans<<endl; } return 0; }