CF821 C. Okabe and Boxes 栈模拟
题意:给出操作,如果当前出栈操作使得出栈序列非顺序,可以在此之前自由排序栈中所有数,问最少排几次。
思路:已经出栈到第x个元素时,每次需要排序的操作后,能够保证前x元素出栈有序,否则说明该操作序列根本无法做到有序出栈。所以碰到不合顺序的数,将栈中所有元素出栈一遍即可。
/** @Date : 2017-07-04 15:21:52
* @FileName: C.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; int main()
{ int n;
while(cin >> n)
{ stacks;
char q[10];
int t;
int tmp = 1;
int ans = 0;
for(int i = 0; i < 2 * n; i++)
{
scanf("%s", q);
if(q[0] == 'a')
{
scanf("%d", &t);
s.push(t);
}
else
{
if(s.empty())
{
tmp++;
continue;
}
if(s.top() != tmp)
{
ans++;
while(!s.empty())
s.pop();
}
else s.pop();
tmp++;
}
}
printf("%d\n", ans);
}
return 0;
}
CF821 C. Okabe and Boxes 栈模拟的更多相关文章
- C. Okabe and Boxes 思维 模拟 or 线段树
C. Okabe and Boxes 这个题目是一个有点思维的模拟,当时没有想到, 思维就是这个栈的排序这里,因为每次直接排序肯定会t的,所以不可以这么写,那怎么表示排序呢? 就是直接把栈清空,如果栈 ...
- Codeforces821C Okabe and Boxes 2017-06-28 15:24 35人阅读 评论(0) 收藏
C. Okabe and Boxes time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 821C - Okabe and Boxes
821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- UVALive 7454 Parentheses (栈+模拟)
Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...
- poj1363Rails(栈模拟)
主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...
- 【LintCode·容易】用栈模拟汉诺塔问题
用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...
- 51Nod 1289 大鱼吃小鱼 栈模拟 思路
1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...
随机推荐
- 福大软工1816:Beta(1/7)
Beta 冲刺 (1/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 答辩 组织会议 复习课本 展示GitH ...
- sed条件不修改匹配
sed '/^echo/!s/text/subtext/g' 如果是以echo开始行首的行就不进行替换. 参考sed substitution conditional
- DNS缓存服务器的配置步骤
yum安装bind 编辑主配置文件/etc/named.conf 修改全局配置文件段 listen-on port 53 {172.16.19.45;}; //allow-query ...
- zuoyeQAQ
public class StringAPIDemo { /** * @param args */ public static void main(String[] args) { // TODO A ...
- 各大巨头电商提供的IP库API接口-新浪、搜狐、阿里
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js (不可用)新浪多地域测试方法:http://i ...
- log4j配置独立日志方法
不使用类,而是使用loggerName来创建日志: #json是用java代码创建logger时用name,而不是jsonlog,注意,不需要在rootLogger中再配置,否则其它无关信息也将输出到 ...
- django里的http协议
一个普通的user Begin########## ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__' ...
- js & array to string
js & array to string https://stackoverflow.com/questions/13272406/convert-string-with-commas-to- ...
- Delphi ADOQuery多个参数重复 改编技巧
今天看了多年前的一个帖子,发现回答不合理,有些还将其归为delphi的bug.其实主要是没有灵活应用参数. ADOQUERY查询时,这样不行,结果不正确. WITH ADOQUERY1 DO BEGI ...
- 【UNIX环境高级编程】线程同步
当多个线程共享相同的内存时,需要确保每个线程看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的,那么就不存在一致性问题.同样,如果变量是只读的也不会有一致性问题.但是,当一个线程可 ...