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 ...
随机推荐
- Dubbo 安装监控中心
一.Dubbo 安装服务管理控制台 1.在官方Github下载Dubbo OPS 2.下载incubator-dubbo-ops源码后,解压修改配置文件Zookeeper注册中心地址 3.maven打 ...
- python函数中闭包的概念说明
函数中闭包的概念说明 闭包: 内层函数对外层函数非全局变量的引用,就叫做闭包 判断闭包方法 ._closure_ : 执行后返回有效信息就是闭包,返回none就不是闭包 举例1: 是闭包 def wr ...
- IoC和AOP扩展
一.构造注入 二.使用p命名空间注入属性值 三.注入不同数据类型 <?xml version="1.0" encoding="UTF-8"?> &l ...
- 虚拟机(unbutun16.04)设置静态ip
电脑上装了虚拟机,想用xshell连接,无奈按照默认的网络设置方式每次重启了虚拟机后都要修改ip才能访问,这怎么能忍,经过一番折腾终于搞定这个问题了,解决步骤如下: 大步骤分为两步:其一是主机的设置, ...
- 将图片绘制到画布上:imagecopy()
<?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...
- Opportunity Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; public class OpportunityHel ...
- flume搭建新手测试环境
硬件环境: 腾讯云,两台服务器8G 双核 软件环境: flume1.8.jdk1.8,centos6 第一次搭建也是各种找文件,只知道flume是日志抓取服务,也听说了非常稳定强大的服务,正好公司需要 ...
- reids同步机制和远程连接
RDB同步机制: 开启和关闭:默认情况下是开启了.如果想关闭,那么注释掉redis.conf文件中的所有save选项就可以了. 同步机制: save 900 1:如果在900s以内发生了1次数据更新操 ...
- python代理爬取存入csv文件
爬取高匿代理 from urllib import request import re import time f = open('西1.csv','w',encoding='GBK') header ...
- 论文翻译第二弹--用python(或Markdown)对论文复制文本进行处理
图中这种论文你想进行文本复制放入翻译软件进行翻译时,会发现是这种形式: 句子之间是断开的,这时普遍的方法,也是我之前一直用的方法就是打开一个文档编辑器,复制上去后一行行地继续调整.昨天不想这样了,就打 ...