D. Psychos in a Line
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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个人,每个人有个权值a[i],如果当前a[i]>a[i+1]则可以杀死右边的人,可以同时杀死;
   求无法杀人的时间;
思路:dp[i]表示i可以杀死右边的人需要的时间;
   从后往前
   需要维护一个单调递减的序列;
   如果你右边的人dp[i+1]需要t时间;
   然后你杀死它只需要一秒,你需要t时间替他杀完那多出来的人;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
int a[N],dp[N];
int q[N];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int head=,tail=;
int ans=;
for(int i=n;i>=;i--)
{
int sum=;
while(tail<=head&&a[i]>a[q[head]])
{
sum++;
sum=max(sum,dp[q[head]]);
dp[i]=max(sum,dp[q[head]]);
head--;
}
ans=max(ans,dp[i]);
q[++head]=i;
}
printf("%d\n",ans);
}
return ;
}
/*
10
10 7 4 2 5 8 9 6 3 1
4 2 1 0 0 0 1 1 1 0
4
*/
 

Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp的更多相关文章

  1. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  2. Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈,递推)

    Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 题意: 你是一名建筑工程师,现给出 n 幢建筑的预计建设高度,你想建成峰状, ...

  3. Codeforces Round #189 (Div. 1 + Div. 2)

    A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...

  4. VK Cup 2016 - Round 1 (Div. 2 Edition) E. Bear and Contribution 单调队列

    E. Bear and Contribution 题目连接: http://www.codeforces.com/contest/658/problem/E Description Codeforce ...

  5. Codeforces Round #189 (Div. 2)

    题目地址:http://codeforces.com/contest/320 第一题:基本题,判断mod 1000,mod 100.,mod 10是不是等于144.14.1,直到为0 代码如下: #i ...

  6. Codeforces Round #189 (Div. 2) A. Magic Numbers

    #include <iostream> #include <vector> #include <algorithm> #include <string> ...

  7. Codeforces Round #189 (Div. 1) C - Kalila and Dimna in the Logging Industry 斜率优化dp

    C - Kalila and Dimna in the Logging Industry 很容易能得到状态转移方程 dp[ i ] = min( dp[ j ] + b[ j ] * a[ i ] ) ...

  8. Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】

    A. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #466 (Div. 2) -A. Points on the line

    2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...

随机推荐

  1. Andrew Ng-ML-第七章-逻辑回归

    1.极大似然估计-逻辑回归代价函数的简化 Andrew Ng的ML视频上讲到:逻辑回归的代价函数可以用最大似然估计法进行简化成上图中第二个式子. 所以学习了一下极大似然估计原理: 2.求偏导 逻辑回归 ...

  2. mysql 简单主从

    主服务器master [root@localhost ~]# vim /etc/my.cnf [mysqld] log-bin=mysql-bin #必须开启log-bin server-id=129 ...

  3. openstack部署心得

    官方文档:https://docs.openstack.org/ 个别版本有中文 不要轻易尝试最新版本 新版本刚推出一般存在不少BUG或者文档没有更新,按照文档配置就是不能成功.推荐尝试最新版本的上一 ...

  4. Google面试题[一]

    谷歌是不少IT人都想去的企业,那么在进入公司前,少不了面试笔试的测试.那么这里我们就总结了如下谷歌笔试题,并提供了一些参考答案.希望对您有用. 谷歌笔试题:判断一个自然数是否是某个数的平方.当然不能使 ...

  5. 软件包管理:rpm命令管理-查询

    yum没有rpm查询的多. 使用包名时无所谓使用位置.因为查询是在后台数据库中查询的. 记忆是只需知道安装和升级时要加包全名,其余加包名.其实未安装的加包全名. rpm包可以手工指定安装路径,但不建议 ...

  6. js 参数声明用var和不用var的区别

    var 声明的变量,作用域是当前 function 没有声明的变量,直接赋值的话, 会自动创建变量 ,但作用域是全局的. //----------------- function doSth() { ...

  7. 微信小程序- wx.request请求不到数据

    小程序官方文档手册 https://mp.weixin.qq.com/debug/wxadoc/dev/ 小程序开发问答社区 http://www.henkuai.com/forum.php wx.r ...

  8. JS多重判断 / ES6 includes

    Array.includes ()  判断数组是否包含某个元素 直接返回true或者false表示是否包含元素,对NaN一样能有有效 const arr = ['1', '2', 'a', 'b' , ...

  9. Oracle提示错误消息ORA-28001: the password has expired

    首先,以SYS DBA身份进入sqlpuls 打开sqlpuls SQL->请输入用户名:sys as sysdba SQL->密码: 登录成功以后查询DBA用户状态 SQL->se ...

  10. maven nexus deploy方式以及相关注意事项(增加eclipse执行maven deploy)

    以前公司都是配管负责管理jar的,现在没有专职配管了,得自己部署到deploy上供使用.总的来说,jar部署到nexus上有两种方式: 1.直接登录nexus控制台进行上传,如下: 但是,某些仓库可能 ...