Codeforces Round #189 (Div. 1) B. Psychos in a Line
2016-05-20 15:38
375 查看
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
Output
Input
Output
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;
}
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;
}
相关文章推荐
- linux有名管道的一点理解
- android camera YV12数据旋转方法
- tomcat配置
- NAT原理与NAT穿越
- 深入理解Java:内省(Introspector)
- 通用java程序linux启动脚本精简版
- 当前线程查看
- 海淘站点优选
- NODEJS 在Centos下面的自动启动
- for循环里的值添加到数组后在循环外打印数组为空
- TF-IDF提取关键词
- ganglia与nagios组合使用
- 获取系统版本
- Freemarker中页面直接可以运用的内置对象
- MySQL存储过程调试工具
- 在GridView控件内文本框实现TextChanged事件,勾选复选框时 :textbox文本框可编辑,编辑文本框的数字后 总金额会重新计算并统计
- 课堂作业:评估手机输入法——讯飞
- headroom.js –在不需要页头时将其隐藏
- java验证码生成代码
- php7在centos上源码安装