#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>#include<cstdlib>#include<string>#include<bitset>#include<iomanip>#include<deque>#include<utility>#defineINF 1000000000#defineFi first#defineSe Second#defineN 2000005#defineP 1000000007#defineDebug (x) cerr<< #x << "=" <<x<<endl#defineMP (x, y) make_pair (x, y)using namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;Char_ch;inlinevoidGetintint&_x) {_x=0; for(_ch=getchar ();_ch>'9'|| _ch<'0'; _ch=GetChar ()); for(; _ch<='9'&&_ch>='0'; _ch=GetChar ()) _x=_x*Ten+_ch- -;}intStk1[n],top1,stk2[n],top2,ans,n;//Stk1 is the current monotonic stack, stk2 the element that will be placed in the STK1//in the STK1, the stk2 is monotonous and non-ascending.intA;voidClear ()//time complexity can also keep O (n){ intI,now,val; while(TOP1) {Val=STK1[TOP1]; for(i=top1;i;i--) if(stk1[top1]!=stk1[i-1]) Break; now=top1-i+1; Top1=i-1; while(now/2)//22 merged into a larger{stk1[++top1]=val+1; now-=2; } //for (int j=1;j<=top1;j++)//Debug (Stk1[j]); //cout<<endl; //Ans=max (ans,stk1[1]);} ans=max (ans,stk1[1]);}intMain () {intI,j,now,val; Freopen ("vincent.in","R", stdin); Freopen ("Vincent.out","W", stdout); CIN>>N; for(i=1; i<=n;i++) {getint (a); stk2[++top2]=A; while(TOP2)//total of 5 cases { if(!TOP1)//If the first stack is empty { //if (top2>1)//top2--; //Elsestk1[++top1]=stk2[top2--]; } Else { if(STK1[TOP1]>STK2[TOP2])//if the top of the first stack is greater than the second stack top//if (top2>1)//If a second stack//while (--TOP1);//Clear//Elsestk1[++top1]=stk2[top2--]; Else if(STK1[TOP1]==STK2[TOP2])//if the top of the first stack equals the second top of the stack /*if (top2>1)//combine two elements into a large {top2--; stk1[top1]++; } else*/{stk1[++top1]=stk2[top2--];//put it in the same place first . } Else if(stk1[top1]<STK2[TOP2]) { if(top1>1&&stk1[top1]==stk1[top1-1])//greedy Merge stack 1 top two elements and put in stack 2{stk2[++top2]=stk1[top1]+1; Top1-=2; } Else{TOP1--; for(j=2; j<=top2;j++)//before emptying, place elements from part of stack 2 inside stack 1stk1[++top1]=Stk2[j]; Clear ();//it can only be emptied out. } } } //for (int j=1;j<=top1;j++)//Debug (Stk1[j]); //for (int j=1;j<=top2;j++)//Debug (Stk2[j]); //cout<<endl; } //for (int j=1;j<=top1;j++)//Debug (Stk1[j]);ans=Max (ANS,STK1[TOP1]); } //For (i=1;i<=top1;i++)//Debug (Stk1[i]); //cout<<endl; //Debug (TOP1);Clear (); cout<<ans<<Endl; return 0;}//DAVIDLEE1999WTK 2016///SrO Myk Orz//Ios::sync_with_stdio (false);//#pragma COMMENT (linker, "/stack:102400000,102400000") compiler C++,not g++/*164 4 4 3 3 3 1 1 2*/
Vicent and games in the bustling simulation game