Psychos in a Line CodeForces - 320D

There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step.

You’re given the initial arrangement of the psychos in the line. Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment. Look notes to understand the statement more precise.

Input
The first line of input contains integer n denoting the number of psychos, (1 ≤ n ≤ 105). In the second line there will be a list of n space separated distinct integers each in range 1 to n, inclusive — ids of the psychos in the line from left to right.

Output
Print the number of steps, so that the line remains the same afterward.

Examples
Input
10
10 9 7 8 6 5 3 4 2 1
Output
2
Input
6
1 2 3 4 5 6
Output
0
Note
In the first sample line of the psychos transforms as follows: [10 9 7 8 6 5 3 4 2 1]  →  [10 8 4]  →  [10]. So, there are two steps.
补一下昨天晚上的训练题。
这个题意很好理解,就跟约瑟夫问题一样,问最后剩下了谁。但是这个题目是问需要操作几次。一开始没有多想,就是按着题意个要求去模拟,果断tle了。莫得办法,想了好久弄得我头皮发麻。搜了一下题解,是采用队列的原理。只要左边的大于右边的,就将它弄入队列,等待被杀。如果这个元素大于队首的元素,就一直杀,一直到没有比他大的就可以了。然后再入队,重复这个过程。
代码如下(ac代码):

#include<bits/stdc++.h>
using namespace std;

const int maxx=1e5+10;
int cur[maxx];
int v[maxx];
int d[maxx];
int n;

void init()
{
	memset(cur,0,sizeof(cur));
	memset(v,0,sizeof(v));
	memset(d,0,sizeof(d));
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		init();
		for(int i=1;i<=n;i++) scanf("%d",&v[i]);
		int cnt=0;
		int ans=0;
		for(int i=n;i>0;i--)
		{
			while(cnt&&v[cur[cnt-1]]<v[i])
			{
				d[i]=max(d[i]+1,d[cur[--cnt]]);
			}
			cur[cnt++]=i;
			ans=max(ans,d[i]);
		}
		cout<<ans<<endl;
	}
	
}

超时代码(提供思路):

/*#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxx=1e5+10;
int a[maxx];
int n;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		int cnt=0;
		
		while(1)
		{
			int flag=1;
			for(int i=n-1;i>=1;i--)
			{
				if(a[i-1]>a[i]) 
				{
					a[i]=0;
					flag=0;	
				}
			}
			if(flag) break;
			cnt++;
			int ant=0;
			for(int i=0;i<n;i++)
			{
				if(a[i]) a[ant++]=a[i];
			}
			n=ant;
		}
		printf("%d\n",cnt);
	}
}

//10 9 7 8 6 5 3 4 2 1*/

努力加油a啊,(o)/~

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/84670852