CodeForces 319B

来源:互联网 发布:利达华信主板数据备份 编辑:程序博客网 时间:2024/05/29 18:54
有编号为1~N的N个人站成一排(顺序是乱的),每人手中拿着一把枪。每一个时刻,每个人同时瞄准右边的人(如
 
果存在的话),然后如果这个人编号比自己大,则开枪将他打死。因此,一个人可能在开枪的同时被打死。一轮开
 
枪结束后,死亡的人视为离开了队列。那么经过多少时刻才能使得整个序列进入稳定模式—— 即不再有人死亡呢?

Input

第一行是N(<=100000),然后N个数,表示初始的站序。

Output

一行一个正整数,表示需要的时刻(即多少轮同时开枪)。此输出应该为最早满足如下条件的时刻:此时的状态应
 
当等同于此时刻以后任意时刻的状态。

Sample Input

10
10 9 7 8 6 5 3 4 2 1
 

Sample Output

2

Hint

[10 9 7 8 6 5 3 4 2 1]→[10 8 4]→[10]

不知道怎么回事头很晕,从最后面开始遍历。脑袋不够清楚,明天仔细想清楚。

#include<iostream>#include<cstdio>#include<algorithm>#include<vector>using namespace std;vector<int> v;vector<int>::iterator it,it2;const int N=100500;int a[N];int nex[N];int main(){//freopen("in.txt","r",stdin);int n;cin>>n;a[0]=0;a[n+1]=n+1;for(int i=1;i<=n;i++){scanf("%d",&a[i]);nex[i]=i+1;if(a[i]<a[i-1]) v.push_back(i-1);}int step=0;while(!v.empty()){++step;it2=it=v.end();it2--;bool flag=true;do{it=it2;if(it2!=v.begin())it2--;int now=*it,now2=*it2;nex[now]=nex[nex[now]];if(it==v.begin()) flag=false;if(a[now] < a[nex[now]] || (it != v.begin() && nex[now2] == now)) v.erase(it);}while(flag);}cout<<step<<endl;return 0;}

0 0
原创粉丝点击