Codeforces Round #189 (Div. 1) B. Psychos in a Line

185 篇文章 0 订阅
116 篇文章 0 订阅

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.


题意:有n个神经病(什么鬼),每一轮如果一个精神病右边的精神病的编号小于他,他就会杀掉他右边的小伙伴,问你最少过几轮不再死人。


分析:设f[i]为第i个精神病杀掉的人数,则答案就等于最大的f[i],但是如何求f[i]呢?当i的权值小于i+1时很显然f[i] = 0,否则i可以在f[i+1]轮内直接替换掉i+1,在f[i+1]轮外我们可以

维护一个单调栈来求f[i]的值。

#include <cstdio>
#include <iostream>
using namespace std;
int n,ans,a[100005],q[100005],f[100005];
bool flag;
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	int t = 0;
	for(int i = n;i;i--)
	{
		q[++t] = a[i];
		while(t-1 && q[t] > q[t-1]) 
		{
			if(f[q[t-1]] > f[q[t]]) f[q[t]] = f[q[t-1]];
			else f[q[t]]++;
			q[t-1] = q[t--];
		}
		ans = max(ans,f[q[t]]);
	}
	cout<<ans<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值