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/problem/319/B
Description
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..
Sample Input
10
10 9 7 8 6 5 3 4 2 1
Sample Output
2
HINT
题意
给你一堆数,每个数次都可以吃掉他右边连续减小的序列,问你多少轮之后,会变成上升序列
题解:
一开始想的暴力,用并查集/链表优化一下,结果不幸被t(其实也是情理之中……
T的数据是 100000 1 2 3 4 5…… 99999
正解是单调队列,维护一个单调下降的序列,就可以找到一直杀到哪儿的位置。
f[i]表示杀人的次数
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1000005
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int a[maxn];
int f[maxn];
int t[maxn];
int main()
{
int n=read(),top=,ans=;
for(int i=;i<n;i++)
a[i]=read();
for(int i=n-;i>=;i--)
{
int tt=;
while(top&&a[t[top-]]<a[i])
f[i]=tt=max(tt+,f[t[--top]]);
t[top++]=i;
}
printf("%d",*max_element(f,f+n));
}
Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列的更多相关文章
- Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp
D. Psychos in a Line time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列
B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...
- 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 ...
随机推荐
- 《C++ primer》--第1,2章小结
来源:http://blog.csdn.net/wangqiulin123456/article/details/8483853 1.变量初始化: 定义变量时,应该给变量赋初始值,除非确定将 ...
- Bat 循環執行範例
@echo off @echo Please key in runcount num. Info:max=100 set /p a= for /l %%i in (1,1,%a%) do ( echo ...
- Golang连接Oracle数据库
Golang连接Oracle的库有很多,比较常见的如下: 不过,oralce 只提供了 oci8 的接口,必须通过它来调用,所以下面方案都逃不过相关设置. 1.go-db-oracle 地址: htt ...
- 分享我的PL/SQL的优化设置,为开发全面提速
打开[工具]–[首选项]: 1.登陆历史:勾选[存储历史]和[带口令存储],方便下次登陆,免去每次都输入密码的烦恼: 2.编辑器: a.勾选[语法高亮]允许: b.关键词大小写选择大写: c.配置自动 ...
- django-grappelli 安装配置
在python34/scripts文件夹下pip3 install django-grappelli pip安装一般会装在python34的tools或lib之类的文件夹下,一定要找到那个文件夹,gr ...
- 任务(Tasks)
在Eclipse中用TODO标签管理任务,利用这个功能可以方便地将项目中一些需要处理的任务记录下来.我们可以通过在java注释里添加任务标签来标记一个任务,任务可以通过Tasks(任务)视图来察看. ...
- win8 VS控件信息
<TextBlock x:Name="button_1" HorizontalAlignment="Center" TextWrapping=" ...
- NServiceBus教程-持久化配置
当配置在NServiceBus v5持久性,秩序是非常重要的.最后赢得持久性配置选项.我们看看一些例子. 示例1 在本例中最后一个配置选项将会覆盖前面的所有选项. v5.2 v5.0 编辑 var c ...
- c# 运行时替换某文件源代码(将XML 转换成 某个枚举并写入源文件)
var sr = new StreamReader(Server.MapPath("~/WEB-INF/rule.config")); XmlDocument doc = new ...
- incompatible