일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Algorithm
- 마일리지
- persistent segment tree
- 대수학
- 이베이트코리아
- 이베이트
- BOJ
- 이베이트미국
- subgroup
- 알고리즘
- persistent indexed tree
- 아시아나
- indexed tree
- gallian
- self balancing binary search tree
- Algebraic Geometry
- Ebate Korea
- acmicpc
- Codeforces
- 대한항공
- finite group
- ccw
- 백준
- Ebate USA
- algebra
- round 424
- round 420
- k번째 수
- 7469
- 구간쿼리
- Today
- Total
[Round#420 Div.2] C. Okabe and Boxes 본문
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.
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.
Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.
3
add 1
remove
add 2
add 3
remove
remove
1
7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove
2
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할 수 없는 경우가 생긴다면 기존에 쌓여있는 숫자들을 내가 원하는 대로 섞을 수 있다.
따라서 어떻게 섞는것이 최선의 방법인지는 추후에 뽑을 때 상황을 봐야 하므로, 일단 우리가 할 수 있는건 현재 쌓여있는 숫자들을 섞는다는건 언제든지 내맘대로 뽑을 수 있다는 것과 마찬가지이다.
즉, 정렬을 한다는 얘기는 스택에 있는것을 모두 뽑아 따로 한쪽에 모아놓고 다음 스탭을 진행한다는 의미와 같다.
'Algorithm > CodeForces' 카테고리의 다른 글
[Round#424 Div2] C.Jury Marks (0) | 2017.07.15 |
---|---|
[Round#420 Div.2] E. Okabe and El Psy Kongroo (0) | 2017.06.26 |
[Round#420 Div.2] B. Okabe and Banana Trees (0) | 2017.06.26 |
[Round#419 Div.2] D. Karen and Test (0) | 2017.06.21 |
[Round#405 Div.2] B. Bear and Friendship Condition (0) | 2017.03.19 |