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(精确的).
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.
Print the number of steps, so that the line remains the same afterward.
10 10 9 7 8 6 5 3 4 2 1
2
6 1 2 3 4 5 6
0
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;
}