Monotonous stack template title luogu P2659

Topic links: https://www.luogu.org/problem/P2659

In fact, we determined to both sides of each digital number is less than its first position, opening the interval length and then multiplied by two positions where the figure, take the maximum output.

Because it is less than the first sides seeking a [i] is the digital, we get a increment (in fact it is not reduced, there may be elements of the same size) monotonically stack, the stack from the bottom to the top of the stack is a digital successively larger in the process of seeking to remain in increments of nature. Every time we find at the left of the first digit numeric stack is less than its numerical position, at the time of obtaining the right of the first stack is less than its numerical position, why this is so, see below, I am here to save the stack It is an element index, instead of element values.

L [i] is less than the first record left a [i] is the element position (subscript), is not 0, R [i] is less than the first record to the right of a [i] is the position of the element, is not n + 1.

  For a a [i], if a [i] is greater than the top element (I stack index is stored, a comparison we took the subscript value comparison), then if the a [i] onto stack, this stack is increasing, this is the first time the top element is less than a [i] number, we directly to the L [i] assigned to the top element on the line.

  In the case of a [i] is equal to the top element, if said values ​​are 5 well, then 5 when it actually pushed onto the stack, which stack the array values ​​L (which is less than its left side a first element position ) we will get to it first, then the current of a [i] (also 5), it's certainly a value equal to L L array array values ​​the top element of it, as the men were equal, directly to a [i] of L value assigned to the array element of the stack array value on it, in the a [i] stack.

  If a [i] is less than the top element, so that if we put a [i] onto the stack, then the stack of elements which do not increase monotonically, so that we need a large stack of elements inside than a [i] will pop up, and now if we want to eject the current top of the stack, so in fact now a [i] is the first value is less than the top element of the right of the top of the stack, so this time we can directly to the R [s.top () ] assigned to i, so whenever we pop an element, left and right sides of this element is less than the first position to its elements are found (on the left in the push to find).

Then, he is gone.

I wrote it myself board, wood should have a problem, that is, slow points:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<set>
#include<cstdio>
#include<string>
#include<deque> 
using namespace std;
typedef long long LL;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define maxn 2000005
stack<int>q;
int n,m,k,t;
int a[maxn];
int L[maxn],R[maxn]; 
inline int read(){
    int f=1,x=0;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do{x=x*10+ch-'0';ch=getchar();}while(ch>=' 0 ' && CH <= ' . 9 ' );
     return F * X; 
} 
int main () 
{ 
    n- = Read ();
     for ( int I = . 1 ; I <= n-; I ++ ) 
    A [I] = Read () ;
     for ( int I = . 1 ; I <; = n-I ++) { // linear sweep over 
        the while (A [I]! q.empty () && A [q.top ()])> { // check the stack where the element is greater than a [i] is 
            int S = q.top ();     // save the value at the top element (subscript) 
            R & lt [S] = I;             //Resulting stack is less than a first element on the right superscript its elements 
            q.pop ();         // pop 
        } 
        
        IF (q.empty ())         // if the stack is empty 
        L [I] = 0 ;
         the else  IF (A [q.top ()] == a [I])     // if the top element is equal to a [i], then L [i] is assigned to the top element of the array value L 
        L [i] = L [Q. Top ()];
         the else                         // top of the stack is less than a [i], the a [i] Drawing 
        L [I] = q.top (); 
        
        q.push (I); 
    } 
    the while (q.empty (! )) { // the last element of the stack without the stack may also, at this time their values are array R n + 1, because the right is not less than the value of their 
        int S = q.top (); 
        R [S ] = n-+ . 1;
        q.pop();
    }
    LL ans=0;
    for(int i=1;i<=n;i++)
    ans=max(ans,(LL)a[i]*(R[i]-L[i]-1)); 
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/6262369sss/p/11257259.html