B. Psychos in a Line

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/319/B

Description

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..

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 单调队列的更多相关文章

  1. 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 ...

  2. Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列

    B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...

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

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

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

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

  5. 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 ...

  6. Codeforces Round #189 (Div. 2)

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

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

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

  8. 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 ] ) ...

  9. 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 ...

随机推荐

  1. SQL Server 最小化日志操作解析,应用[手稿]

    Sql Server 中数据库在BULK_LOGGED/SIMPLE模式下的一些操作会采用最小化日志的记录方式,以减小tran log落盘日志量从而提高整体性能. 这里我简单介绍下哪些操作在什么样的情 ...

  2. Java核心 --- 泛型

    CoreJava 泛型 java泛型的出现避免了强制类型转换,便于代码更好的被阅读 本文的写作参照了张孝祥的泛型介绍:http://www.itcast.cn/news/dbfd20f1/f4b1/4 ...

  3. iOS学习中的一些误区

    周二拿到offer之后,周三确定了去哪家,今天周四.今天是一个例外.中午写更新. 人到了不同的阶段,最重要的就是要更新自己的方法论.也就是说,不能穿新鞋,走老路,这样人就不会有大的突破. 下面我就分析 ...

  4. HDU5875:Function

    题目链接: Function 分析: icpccamp里的方法不会,我用了一个nex[]数组存储当前点ai需要取模的下一个点aj的编号j,如果aj>ai,就不用遍历. 时间为920ms 代码: ...

  5. java 的开源wiki维基系统

    几乎所有 维基 系统的对比网址:   http://www.wikimatrix.org/ XWiki,    第二代wiki. 它里面使用的 velocity 模板语言对j2ee开发相当有参考价值, ...

  6. python 字符串,数组,元祖操作基础巩固。

    由于上个星期有点忙,没时间来抽空记一些有用的东西.丢了比较久的python很多忘记的小操作我也会重新捡起来 以前最容易搞混的 str.split() #操作会生成一个数组对象.example:'lap ...

  7. Northwind数据库表字段介绍

    ① Categories:种类表 相应字段: CategoryID :类型ID: CategoryName:类型名; Description:类型说明; Picture:产品样本 ② Customer ...

  8. 应用引擎BAE3.0(转)

    add by zhj: 其实我主要是想看看基于docker的PaaS的特性. 原文:http://developer.baidu.com/wiki/index.php?title=docs/cplat ...

  9. Cisco Router WEB管理

    目前市场上很多思科路由器或者交换机都可以通过WEB方式配置.尽管很多功能还是只能通过CLI配置,但是一些功能还是很有用的,例如端口的流量监控功能 前期准备: 一.设备的IOS要支持WEB管理功能   ...

  10. UIView UITableView 背景图片添加

    这几天,经常用到为某个视图设置背景图片,而API中UIView没有设置背景图片的方法,搜集归纳如下: 第一种方法: 利用的UIView的设置背景颜色方法,用图片做图案颜色,然后传给背景颜色. UICo ...