codeforces 420 div2 C Okabe and Boxes

来源:互联网 发布:mac 移动硬盘 win10 编辑:程序博客网 时间:2024/04/20 22:24

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
3add 1removeadd 2add 3removeremove
output
1
input
7add 3add 2add 1removeadd 4removeremoveremoveadd 6add 7add 5removeremoveremove
output
2
我就觉得CF出的题都很刁钻,考思维比较多。

对于这道题目,题目保证了在需要移除num时,num一定是已经被压入了,现在,如果num就在栈的最顶端,那么毫无疑问可以直接移除,不需要任何耗费,而一旦不在最顶端,那么必然要经过一次排序,从而把这个num  排到最顶端,剩下的元素的顺序可以完全按照我们心意来排序。我们考虑这样一种情况就是说

add 1 

add 3

add 2

remove

remove

remove

这样的话,在第一次排序完成以后,我们直接移除1,之后,需要移除的num变成了2,而并没有新元素的加入,所以我们可以在上次排序的时候把这次要移除的那个元素(也就是2)排到第二个位置。后面还有一次移除(3),从而也不需要额外的耗费就可以进行移除。

所以,我们可以得到规律,那就是当我们 add一个新元素的时候,栈顶一定会改变的,reorder之后连续进行的移除操作不需要额外的耗费(只需要1个coin)

那么我们可以写出程序:

每add一个元素num,我就把他压入栈中,然后把当前栈顶修改。now = num

注意now为0时候表示特殊含义,此时也要把now压到栈里面去

移除的时候分两种情况,

(1)now刚好是要移除的(now == rs)那么就

rs++;now = stk.top;stk.pop();

(2)now是0,表示连续移除,那么随便移除,从栈里面往外拿就ok了

(3)否则的话就要进行reorder了,然后设置now为0表示可以进行连续移除

#include <iostream>#include <stack>#include <cstdio>using namespace std;char str[10];int main(){int N;scanf("%d",&N);stack<int> stk;int rq = 1;int now = -1;int ans = 0;for(int i = 0;i < 2*N;i++){int num;scanf("%s",str);if(str[0] == 'a'){if(now == 0)stk.push(now);scanf("%d",&now);stk.push(now);}else{if(now == rq){rq ++;stk.pop();now = stk.empty()?-1:stk.top();}else if(now == 0){rq++;stk.pop();}else{rq++;ans++;now = 0;stk.pop();} }}cout<<ans<<endl;return 0;}



原创粉丝点击