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. VMware Workstation 安装Vmware tools 是 出现vmware tools unavailable

    这个问题是因为虚拟机安装的时候操作系统选择的不对,在Virtual Machine Settings中选择Options,在General中选择正确的操作系统类型 例如Guest operating ...

  2. 500. Keyboard Row (5月26日)

    解答 class Solution { public: vector<string> findWords(vector<string>& words) { vector ...

  3. FMX相关

    ListView的ItemAppearance的样式效果表: Navicat for 插入图片步骤: 如果最后一条记录的图片有问题,可以先插入下一条再导入图片.

  4. while 循环,格式化输出和运算编码

    今日内容 1.while循环 while Ture:             content = input ("请输入你要喷的内容",输入Q退出)             if ...

  5. python+selenium实现自动抢票

    使用说明 程序运行开始,需要输入出发地,目的地,出发时间,乘客信息,车次:乘客信息和车次可以输入多个 刚刚开始学习爬虫,selenium仅仅是解放了双手,运行效率不是很高: 程序运行时会打开chrom ...

  6. Java学习笔记二十六:Java多态中的引用类型转换

    Java多态中的引用类型转换 引用类型转换: 1.向上类型转换(隐式/自动类型转换),是小类型到大类型的转换: 2.向下类型转换(强制类型转换),是大类型到小类型的转换: 3.instanceof运算 ...

  7. 数组循环左移 i 位

    数组左移 i 位 3 种方法 1.临时数组存储 先将前 i 个元素用数组存起来 再将后 n - i 个元素左移 i 位 最后将存起来的数组添加到后面去即可 2.通过多次调用左移 1 位的函数 3.翻转 ...

  8. LVS那些事

    1998年,章文嵩博士成立了Linux Virtual Server的自由软件项目,进行Linux服务器集群的开发工作. 他的目标是:使用集群技术和Linux操作系统实现一个高性能.高可用的服务器,它 ...

  9. vim 版本更新

    sudo add-apt-repository ppa:jonathonf/vim sudo apt update sudo apt install vim 如果您想要卸载它, 请使用如下命令 sud ...

  10. Codeforces Contest 870 前三题KEY

    A. Search for Pretty Integers: 题目传送门 题目大意:给定N和M个数,从前一个数列和后一个数列中各取一个数,求最小值,相同算一位数. 一道水题,读入A.B数组后枚举i.j ...