Monotonous stack entry title

Monotonous stack of questions, do not know the template. . .

Thought it is important to ensure that the stack element is incremented, monotonicity is destroyed once been taken out of the stack element and calculated until the monotone, the success of a complex calculation with the simplified analog conversion.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;

const int mx = 1e6 + 50;
const int inf = 1e9;

inline void fre(){ freopen("sign1.in", "r", stdout); freopen("sign1.out", "w", stdout); }

ll st[mx], top, w[mx], a[mx];
int n;

int main(){
    //fre();
    while(scanf("%d", &n) == 1 && n) {
        top = 0;
        ll ans = 0;
        for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        a[n+1] = 0;
        
        for(int i = 1; i <= n+1; i++) {
            if(a[i] > st[top]) st[++top] = a[i], w[top] = 1;
            else{
                int width = 0;
                while(a[i] <= st[top] && top){
                    width += w[top];
                    ans = max(ans, (ll)width * st[top]);
                    top--;
                }
                st[++top] = a[i]; w[top] = width+1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}




Guess you like

Origin www.cnblogs.com/Maktub-blog/p/11443326.html