Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp
1 second
256 megabytes
standard input
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.
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.
#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的更多相关文章
- 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 ...
- Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈,递推)
Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 题意: 你是一名建筑工程师,现给出 n 幢建筑的预计建设高度,你想建成峰状, ...
- Codeforces Round #189 (Div. 1 + Div. 2)
A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...
- 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 ...
- Codeforces Round #189 (Div. 2)
题目地址:http://codeforces.com/contest/320 第一题:基本题,判断mod 1000,mod 100.,mod 10是不是等于144.14.1,直到为0 代码如下: #i ...
- Codeforces Round #189 (Div. 2) A. Magic Numbers
#include <iostream> #include <vector> #include <algorithm> #include <string> ...
- 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 ] ) ...
- 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 ...
- 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 ...
随机推荐
- Selenium之WebdriverApi详解
获取标签元素 # 通过ID定位目标元素 driver.find_element_by_id('#i1') # 通过classname定位目标元素 driver.find_element_by_clas ...
- 自定义WordPress文件上传路径
自WordPress 3.5版本开始,隐藏了后台媒体设置页面的“默认上传路径和文件的完整URL地址”选项,可以通过下面的代码将该选项调出来. 将下面的代码添加到当前主题functions.php文件中 ...
- Integer.valueOf方法的源码解读
public class IntegerDemo { public static void main(String[] args) { Integer i01 = ; ; Integer i03 = ...
- [py]列表生成式-支持条件,多值的拼接
列表生成式 代码简洁一些 支持多条件, 过滤,或拼接某些值 支持返回多值 是一种生成式 # 生成一个列表 print(list(range(1, 11))) # 生成一个列表x^2 ## 方法1: 返 ...
- 计划任务cmd 清理文件
forfiles.exe /p D:\mysql-back /s /m * /d -30 /c "cmd /c del /q @path" #清理目录下创建大于30天所有类型的文件
- mysql进程文件
- tomcat警告WARNING: An attempt was made to authenticate the locked user "user"
后台出现很多警告WARNING: An attempt was made to authenticate the locked user "user"Jul 19, 2017 2: ...
- Web API设计方法论--比较完整的web api 开发过程
为Web设计.实现和维护API不仅仅是一项挑战:对很多公司来说,这是一项势在必行的任务.本系列将带领读者走过一段旅程,从为API确定业务用例到设计方法论,解决实现难题,并从长远的角度看待在Web上维护 ...
- 向大家分享一个shell脚本的坑
打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中. 先找出其中一台远程机器,查看main进程运行情况 [root@two002 tmp]# ps -ef|grep ...
- jsp/servlet/mysql/linux基本概念和操作
一.什么是OOP编程? 面向对象,以结果为导向,并封装整个过程,并尽可能地增加代码的复用性和可扩展性...... 二.Junit? JUnit是一个java语言的单元测试框架.Junit测试时程序员测 ...