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)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值