Psychos in a Line(单调栈)

Psychos in a Line

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.

题意:

有一排精神病人站着队,每一时刻他都会犯病把在自己右边相邻的比他矮的给杀掉,问经历多少时间之后就不会出现人员的消失了

思路:

就是一个贪心,维护一个单调递减栈,在这个点栈内在这个点之前的点是一定能把这个点给杀死的,咱们开一个nums数组记录第几次被杀死,然后在进行单调栈操作时每遇到一个比他小的都更新一下这个点的nums数组,因为他自己是在这个比它小的点之后被杀死的,然后遇到比他大点的时候,就用nums[i]+1这个值进行更新,因为他能被他前面的人杀死,但是这个时刻还没死,而且下一个时刻一定会死,所以说就用nums[j]+1这个值进行更新,具体看代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int N = 100010;
int s[N],tt,nums[N],a[N];

int main(){
	int n,ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++){
		int cnt = 0;
			while(tt && s[tt] <= a[i]){
				cnt = max(cnt,nums[s[tt]]);
				tt--;
			}
			if(tt)
			nums[a[i]] = cnt + 1;
			s[++tt] = a[i];
	}
	for(int i=1;i<=n;i++) ans = max(ans,nums[a[i]]);
	cout<<ans<<endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宇智波一打七~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值