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 ≤ 10^5). 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.

题目大意:每一ROUND如果某个人的数字大于他右边的人,他就会干掉右边的人,一个人可以同时干掉别人和被干掉,问要多少ROUND结束后才不会死人。

思路:大水题,CF你标什么数据结构……初始化每个人给一个杀人的状态(如果他大于右边的人),如果他不能杀人了就取消他的状态(没有重新获得杀人状态的可能性,这里不证),方便删除用链表记录这些有杀人状态的人,一ROUND一ROUND地模拟即可。

PS:顺便研究了一下list怎么用(迭代器居然不能直接用加号和减号可恶),忘了删调试代码又WA了一次(刚好最后测的那个数据没有把调试代码的东西打出来结果忘了就交上去了>_<)

代码(63MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std; const int MAXN = ; list<int> killing;
list<int>::iterator it, it2; int a[MAXN], next[MAXN];
int n; int main() {
scanf("%d", &n);
for(int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
next[i] = i + ;
if(a[i - ] > a[i]) killing.push_back(i - );
}
a[n + ] = n + ;
int step = ;
while(!killing.empty()) {
++step;
it = it2 = killing.end();
--it2;
bool flag = true;
//for(list<int>::iterator p = killing.begin(); p != killing.end(); ++p) printf("%d\n", a[*p]);
do {
it = it2--;
//printf("%d %d %d\n", *it, *it2, a[*it]);
int now = *it, now2 = *it2;
next[now] = next[next[now]];
if(it == killing.begin()) flag = false;
if(a[now] < a[next[now]] || (it != killing.begin() && next[now2] == now)) killing.erase(it);
} while(flag);
}
printf("%d\n", step);
}

codeforces 319B Psychos in a Line(模拟)的更多相关文章

  1. CF 319B Psychos in a Line 【单调队列】

    维护一个单调下降的队列. 对于每一个人,只需要找到在他前面且离他最近的可以杀掉他的人即可. #include <cstdio> #include <vector> #inclu ...

  2. Psychos in a Line CodeForces - 319B (单调栈的应用)

    Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...

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

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

  5. B. Psychos in a Line 解析(思維、單調棧)

    Codeforce 319 B. Psychos in a Line 解析(思維.單調棧) 今天我們來看看CF319B 題目連結 題目 給一個數列,如果相鄰兩數,左邊大於右邊,那麼就可以殺死右邊的數字 ...

  6. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  7. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Beta Round #3 C. Tic-tac-toe 模拟题

    C. Tic-tac-toe 题目连接: http://www.codeforces.com/contest/3/problem/C Description Certainly, everyone i ...

  9. Codeforces Beta Round #1 B. Spreadsheets 模拟

    B. Spreadsheets 题目连接: http://www.codeforces.com/contest/1/problem/B Description In the popular sprea ...

随机推荐

  1. hdu Hat's Fibonacci(用了kuangbin模板)

    大数的位数设置很坑,设成700会越界,设成800会超空间,最后设成了750居然就过了.... #include <iostream> #include <cstdio> #in ...

  2. OS--lab0+lab1+lab4+lab5+lab6+lab7

    URL:https://github.com/Chasssser/MytestOR(Linux) git clone https://github.com/Chasssser/Mytest

  3. Eclipse切换字体颜色

    打开window-preferences

  4. WSO2 API Manager中host Ip 不正确的问题解决方法

    问题: 根据官方的Quick start的教程,部署完AM后,添加的API的host Ip不正确,为localhost或者服务器上的其他虚拟ip. 安装版本:       WSO2AM 2.6.0 环 ...

  5. collections.ChainMap类合并字典或映射

    ## 使用update()方法或者ChainMap类合并字典或映射 # 使用update()方法合并 a = {'x': 1, 'z': 3} b = {'y': 2, 'z': 4} merged ...

  6. 3D立方体

    效果图 主要用到的3D属性 1.保留子元素的3d属性:transform-style:preserve-3d; 2.2D变形属性: ①transform:translate()平移,分X轴,Y轴,Z轴 ...

  7. python学习笔记:第20天 多继承、MRO C3算法

    目录 一.多继承 二.旧式类的MRO 三.新式类的MRO 四.super 一.多继承 之前已经学习过了继承,当出现了x是⼀种y的的时候,就可以使⽤继承关系,即"is-a" 关系.在 ...

  8. mysql的数据类型与表约束

    数据类型 (详细数据类型请参考:http://www.runoob.com/mysql/mysql-data-types.html) 数字 整型  tinyint int bigint 小数: flo ...

  9. STL---llist

    #include<iostream> #include<algorithm> #include<list> using namespace std; struct ...

  10. C语言程序设计:现代方法(第2版)第二章全部习题答案

    前言 本人在通过<C语言程序设计:现代方法(第2版)>自学C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考.这些答案是本人自己解 ...