codeforces 319B Psychos in a Line(模拟)
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 ≤ 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.
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(模拟)的更多相关文章
- CF 319B Psychos in a Line 【单调队列】
维护一个单调下降的队列. 对于每一个人,只需要找到在他前面且离他最近的可以杀掉他的人即可. #include <cstdio> #include <vector> #inclu ...
- Psychos in a Line CodeForces - 319B (单调栈的应用)
Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...
- 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 #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 ...
- B. Psychos in a Line 解析(思維、單調棧)
Codeforce 319 B. Psychos in a Line 解析(思維.單調棧) 今天我們來看看CF319B 題目連結 題目 給一個數列,如果相鄰兩數,左邊大於右邊,那麼就可以殺死右邊的數字 ...
- Educational Codeforces Round 2 A. Extract Numbers 模拟题
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- 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 ...
- Codeforces Beta Round #3 C. Tic-tac-toe 模拟题
C. Tic-tac-toe 题目连接: http://www.codeforces.com/contest/3/problem/C Description Certainly, everyone i ...
- Codeforces Beta Round #1 B. Spreadsheets 模拟
B. Spreadsheets 题目连接: http://www.codeforces.com/contest/1/problem/B Description In the popular sprea ...
随机推荐
- 奇(qi)谋(ji)巧(yin)计(qiao)
一.打表法 0.http://oeis.org/ 1.差分序列:https://blog.csdn.net/wu_tongtong/article/details/79115921 对于一个多项式产生 ...
- sysbench安装
sysbench安装 1.下载软件mkdir -p /usr/local/softwarecd /usr/local/softwaregit clone https://github.com/akop ...
- jQuery关于复选框的基本小功能
这里是我初步学习jquery后中巨做的一个关于复选框的小功能: 点击右边选项如果勾上,对应的左边三个小项全部选中,反之全不选, 左边只要有一个没选中,右边大项就取消选中,反之左边全部选中的话,左边大项 ...
- hql返回数值
public int getCountUser() throws ParseException { Session hSession = sessionFactory.getCurrentSessio ...
- redis之闪电内幕
一.简介和应用 二.Redis的对象redisObject 三.String 四.List 4.1 linkedlist(双端链表) 4.2 ziplist(压缩列表) 五.Hash 六.Set 七. ...
- 『Linux基础 - 4 』linux常用命令(1)
这篇笔记包含以下知识点: 几个概念的理解:Linux命令,控制台,终端, 终端提示符 对文件目录的操作的相关命令: 切换目录,列出目录下的文件等 对文件的操作的相关命令: 创建,删除,复制,修改,移动 ...
- 如何防止index.html首页被篡改
近期发现公司网站首页文件经常被篡改为indax.php或indax.html,导致网站的功能无法正常使用,百度搜索关键词,在显示结果中点击公司网站,打开后跳转到别的网站上去了,尤其我们在百度做的推广, ...
- Java学习笔记十七:Java中static使用方法
Java中static使用方法 一:Java中的static使用之静态变量: 我们都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的 ...
- STM32 时钟配置的坑
今天在调试公司的一款产品的时候发现8M的晶振用完了,于是找了一个16M的替代 坑爹的就在这里,明明已经把时钟按照时钟树配置好了,但是串口等外设一直无法正常工作 折腾了一下午,终于发现这位老兄的文章ht ...
- 22-Consent 确认逻辑实现
1-定义一个从前台传到后台的viewModel namespace MvcCookieAuthSample.Models { public class InputConsentViewModel { ...