C. Okabe and Boxes
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n.
Initially there are no boxes on the stack.

Okabe, being a control freak, gives Daru 2n commands: n of
which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash.
Okabe wants Daru to throw away the boxes in the order from 1 to n.
Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.

That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.

Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.

Input

The first line of input contains the integer n (1 ≤ n ≤ 3·105) —
the number of boxes.

Each of the next 2n lines of input starts with a string "add"
or "remove". If the line starts with the "add", an integer x (1 ≤ x ≤ n)
follows, indicating that Daru should add the box with number x to the top of the stack.

It is guaranteed that exactly n lines contain "add"
operations, all the boxes added are distinct, and n lines contain "remove"
operations. It is also guaranteed that a box is always added before it is required to be removed.

Output

Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.

Examples
input
3
add 1
remove
add 2
add 3
remove
remove
output
1
input
7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove
output
2
Note

In the first sample, Daru should reorder the boxes after adding box 3 to the stack.

In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to
the stack.

——————————————————————————————

题意:模拟一个栈,有两种操作,栈顶增加一个元素,栈顶移除一个元素,要求元素按照编号大小移除,移除前可以选择是否调整栈内元素顺序,询问最少调整次数

解题思路:开一个数组记录加进去的数,不符合时清空数组

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int a[500005]; int main()
{
int n,x;
char s[100];
while(~scanf("%d",&n))
{
int cnt=0;
int tot=1;
int ans=0;
for(int i=0; i<2*n; i++)
{
scanf("%s",s);
if(strcmp(s,"remove")==0)
{
if(cnt==0)
tot++;
else if(a[cnt-1]==tot)
{
cnt--;
tot++;
}
else
{
cnt=0;
ans++;
tot++;
}
}
else
{
scanf("%d",&x);
a[cnt++]=x;
}
}
printf("%d\n",ans);
}
return 0;
}

Codeforces821C Okabe and Boxes 2017-06-28 15:24 35人阅读 评论(0) 收藏的更多相关文章

  1. POJ 1068 AC 2014-01-07 15:24 146人阅读 评论(0) 收藏

    POJ的题目都是英文的,所以,,,还是直接贴代码吧 #include<stdio.h> int main(){ int x,y,z; int n,nm,max; scanf("% ...

  2. strtok函数 分类: c++ 2014-11-02 15:24 214人阅读 评论(0) 收藏

    strtok函数是cstring文件中的函数 strtok函数是cstring文件中的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...

  3. Codeforces821B Okabe and Banana Trees 2017-06-28 15:18 25人阅读 评论(0) 收藏

    B. Okabe and Banana Trees time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏

    Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  5. Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏

    Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...

  6. Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏

    Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21767   Accepted: 13641 Desc ...

  7. 苹果应用商店AppStore审核中文指南 分类: ios相关 app相关 2015-07-27 15:33 84人阅读 评论(0) 收藏

    目录 1. 条款与条件 2. 功能 3. 元数据.评级与排名 4. 位置 5. 推送通知 6. 游戏中心 7. 广告 8. 商标与商业外观 9. 媒体内容 10. 用户界面 11. 购买与货币 12. ...

  8. Codeforces821A Okabe and Future Gadget Laboratory 2017-06-28 14:55 80人阅读 评论(0) 收藏

    A. Okabe and Future Gadget Laboratory time limit per test 2 seconds memory limit per test 256 megaby ...

  9. Codeforces816A Karen and Morning 2017-06-27 15:11 43人阅读 评论(0) 收藏

    A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

随机推荐

  1. c#devexpress GridContorl添加进度条

    demo 的实现图 下边是步骤和代码 1定义 时钟事件,定时的增加进度条的增量. 2:  添加进度条 3;定义字段属性 using System; using System.Collections.G ...

  2. linux 发送Post请求 json格式

    curl -H "Content-type: application/json" -X POST -d '{"text":"总体来说很不错,环境挺好的 ...

  3. dpdk优化相关 转

    注:本文是参照了一些其他文章,原文地址点击这里. 首先根据这篇文章进行了性能瓶颈的分析 策略与方法 首先根据木桶原理,首先要找到最弱的地方,怎么找往上看↑. 想能优化需要考虑如下: 优化BIOS设置 ...

  4. 如何修改隐藏Zblog/WordPress默认后台登录地址

    我相信很多博主站长都遇到过站点被暴力破解,虽然未被破解,但是经常收到那些尝试登录失败的邮件提醒也会心慌慌的.对于这种情况,最好的办法就是修改/隐藏我们的后台登录地址. 关于zblogASP后台登录地址 ...

  5. 20172325 2017-2018-2 《Java程序设计》第十一周学习总结

    20172325 2017-2018-2 <Java程序设计>第十一周学习总结 教材学习内容总结 Android简介 Android操作系统是一种多用户的Linux系统,每个应用程序作为单 ...

  6. django POST表单的使用

    环境如下:django 1.7.8 版本. 1.在POST表单的时候会出现这个错误提示. 禁止访问 (403) CSRF验证失败. 相应中断. Help Reason given for failur ...

  7. 一种基于URL数据源的WEB报表插件

    完全支持所见所得的报表设计, 支持 PHP ,Java 等所有支持JSON格式的后端. 立即下载测试版本 需要正式版本?请QQ联系:1565498246 或者留言

  8. swift NSdata 转换 nsstring

    result = NSString(data: data, encoding: NSUTF8StringEncoding) 做HTTP 请求时 遇到 打印结果看  所以~~~

  9. ubuntu下安装/卸载vmware虚拟机

    1.下载vmware(官网下载试用版,试用版输入序列号后即为专业版,序列号网上搜,很多) 2.下载后安装(命令行) 1)cd进你下载的位置 1.1)下载的文件名字为:VMware-Workstatio ...

  10. centos7 sqoop 1 搭建笔记

    1.require : java环境,hadoop,hive ,mysql2.下载解压sqoop13.设置环境变量 export SQOOP_HOME=/data/spark/bin/sqoop ex ...