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 datasource为 类字段的实现方式 非datatable方式以及其他操作总结

    1:定义model class A { public string a{get;set;} public string b{get;set;} } 2:赋值: A aobj=new A(); aobj ...

  2. c# 子线程打开子窗体

    下边是在子线程打开子窗口,结果跑到else 里边了跨线程操作窗体控件InvokeRequired失效,无法用于打开子窗体,addonetwo.InvokeRequired,访问不了呢? 大神知道帮忙回 ...

  3. HDU_2112(最短路)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

  4. spring batch遇到的一些问题

    1.Spring Batch - A job instance already exists: JobInstanceAlreadyCompleteException 这是因为JobParameter ...

  5. 简单使用limma做差异分析

    简单使用limma做差异分析 Posted: 五月 12, 2017  Under: Transcriptomics  By Kai  no Comments 首先需要说明的是,limma是一个非常全 ...

  6. 开发apicloud模块遇到的几个梗

    2017-06-04 原来模块中不能的R.id.xxx,只能用UZResourcesIDFinder.getResIdID("mo_minivr_framecontainer") ...

  7. js 事件创建发布

    // 创建事件. var event = document.createEvent('Event'); // 初始化一个点击事件,可以冒泡,无法被取消 event.initEvent('click', ...

  8. 要显示的联系人——>自定义-bug

    自定义中将“电话”下的“所有联系人”不勾选,但是Contacts列表还是显示PHONE联系人. SELECT _id, display_name, agg_presence.mode AS conta ...

  9. Desktop Central帮助您升级Windows 10,获取更新的五大增强功能

  10. rails 数据迁移出问题

    数据migrate重置 rails db:migrate:reset 具体的,,还不清楚,想起来了再去补充