Patrik concert monotonous waiting stack of confused memories

STL must learn have to learn, we must learn! ! !

Topic links: https://www.luogu.org/problemnew/show/P1823

 

We need to find the way; monotonous stack;

Ideas:
maintaining a monotonically decreasing height of the stack, if the next insertion of a shorter than on directly into the stack, if the current insertion of a high ratio, we will update the value of the answer;

Because now people will want to insert the block in front of shorter than him, so the front is shorter than his people no longer see the future of the people;

Record the number of the front and of course his person as high, and as high as he can see the person who is behind him (Subject is not greater than or equal to)

Because we maintain the value of the right point, so the dwarf directly in front of the pop-up;

Code

#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=500050;
stack <int> s;//单调栈 
int n,x,ans;
int a[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int t=1;//His height and recording the number of people like 
        Scanf ( " % D " , & X);
         the while (s.size () && X> = s.top ()) // there was human and stack or the like is shorter than his high 
        {
             IF (X s.top == ()) T ++ ; 
            ANS ++; s.pop (); // because they do not can not view the pop-up on a value, even if the contour should pop 
        }
         IF (s.size ()) ANS ++; // if front of someone taller than him, so the two of them could see each other 
        the while (T--) s.push (the X-); // all people should join the pop-high stack 
    } 
    the printf ( " % D " , ANS); 
    
    return  0 ; 
}

Sorry, TLE

Obviously we are dealing with people's high wasted a lot of time, so we can put in front of people and his contribution to high added directly to the people who now want to insert

With a structure on the line

 

Code

#include <cstdio> 
#include <CString> 
#include <Stack> 
#include <algorithm>
 the using  namespace STD;
 const  int MAXN = 500.05 thousand ; 

struct Node 
{ 
    int H; // height 
    Long  Long NUM; // front contour al number 
}; 
Stack <Node> S;
 int n-, X;
 Long  Long ANS;
 int A [MAXN];
 int main () 
{ 
    Scanf ( " % D " , &n-);
     for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        Scanf ( " % D " , & X); 
            Node P = (Node) {X, . 1 }; // record the current contour and his who own 
        the while (s.size () && X> = s.top () H.) 
        { 
            IF (X == s.top () H.) + = p.num s.top () NUM;. 
            ANS + = . s.top () NUM; s.pop (); 
        } 
        IF (s.size ()) ANS ++ ; 
        s.push (P); 
    
    } 
    the printf ( " % LLD " , ANS);
    
    return 0;
}

 

STL will not, handwritten two lines of tears;

 

Guess you like

Origin www.cnblogs.com/WHFF521/p/10960098.html