[Round#420 Div.2] C. Okabe and Boxes 본문

Algorithm/CodeForces

[Round#420 Div.2] C. Okabe and Boxes

previc 2017. 6. 26. 21:14

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.





add명령어가 나오면 순서대로 스택에 숫자들을 쌓는다.


remove가 나오면 무조건 숫자를 오름차순 순서대로 스택에서 뽑아야한다.


만약 스택에서 뽑을 숫자가 다음 순번의 숫자가 아니라면 임의의 정렬을 통해서 스택의 숫자들을 섞을수 있다.


총 숫자를 뽑을 때까지 필요한 최소 정렬횟수를 구하는문제



스택에 순서대로 쌓고, 빼고를 반복하다가 만약 다음번의 숫자를 remove할 수 없는 경우가 생긴다면 기존에 쌓여있는 숫자들을 내가 원하는 대로 섞을 수 있다.


따라서 어떻게 섞는것이 최선의 방법인지는 추후에 뽑을 때 상황을 봐야 하므로, 일단 우리가 할 수 있는건 현재 쌓여있는 숫자들을 섞는다는건 언제든지 내맘대로 뽑을 수 있다는 것과 마찬가지이다.


즉, 정렬을 한다는 얘기는 스택에 있는것을 모두 뽑아 따로 한쪽에 모아놓고 다음 스탭을 진행한다는 의미와 같다.