Codeforces 319B. Psychos in a Line【单调栈】

题目链接：

http://codeforces.com/problemset/problem/319/B

代码：

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;

int n,a[maxn];

struct node{
int x,y;
}que[maxn];

int main(){
cin >> n;
for(int i=1; i<=n; i++)

int i=2;
while(a[i]>a[i-1]) i++;
int tail = 0, ans = 0, mx=0;
que[tail].x = a[i-1], que[tail++].y = 0;
for( ; i<=n; i++){
if(a[i] < que[tail-1].x){
que[tail].x = a[i], que[tail++].y = 1;
ans = max(ans,que[tail-1].y);
continue;
}
mx = 0;
while(tail>0 && a[i]>que[tail-1].x) mx=max(mx,que[tail-1].y),tail--;
que[tail].x = a[i];
if(tail==0) que[tail++].y=0;
else que[tail++].y=mx+1;
ans = max(ans,que[tail-1].y);
}
cout << ans << endl;

return 0;
}

posted @ 2017-07-28 22:24  _yxg123  阅读(481)  评论(0编辑  收藏  举报