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 ...
随机推荐
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- Vagrant配置虚拟机
慕课上学习.需要安装 vagrant VirtualBox .box文件和.iso文件一样都是镜像文件.可以在官网下载https://www.vagrantup.com/docs/ 点击boxs之后 ...
- 实体框架 Code First
原文:https://msdn.microsoft.com/zh-cn/en-zn/data/jj591621
- A+B+C问题
这是一个非常简单的题目,意在考察你编程的基础能力.千万别想难了哦.输入为一行,包括了用空格分隔的三个整数 AA.BB.CC(数据范围均在-40−40 ~ 4040 之间).输出为一行,为“A+B+CA ...
- Django初级手册3-视图层与URL配置
设计哲学 在Django中一个视图有指定函数和指定模版组成.对于某些特定的应用应该分成若干视图.例如博客系统 Blog主页面 详细页面入口 基于年的页面展示 基于月的页面展示 基于天的页面展示 评论行 ...
- 20155227 2016-2017-2 《Java程序设计》第九周学习总结
20155227 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC简介 JDBC全名Java DataBase Connectivity,是java联 ...
- Azkaban学习笔记(二)
官方文档:http://azkaban.github.io/ 一.Azkaban主要的组成: 1. 关系型数据库——MySQL 2. AzkabanWebServer 3. AzkabanExcuto ...
- 数据仓库基础(十)Informatica 组件1
本文转载自:http://www.cnblogs.com/evencao/p/informatica.html Informatica主要的组件: Source Qualifier 从数据源读取数据 ...
- MySQL笔记(三)由txt文件导入数据
改编自学校实验,涉及一些字符集相关的问题. 索引 建库 导入数据 最终脚本 下载数据 点击这里 建库 create.sql DROP DATABASE IF EXISTS orderdb; CREAT ...
- SNMP学习笔记之SNMP树形结构介绍
Basic command of SNMP: GET: The GET operation is a request sent by the manager to the managed device ...