821C.md (2370B)
1 Source: https://codeforces.com/problemset/problem/821/C 2 3 C. Okabe and Boxes 4 5 time limit per test 6 7 3 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 Okabe, being a control freak, gives Daru 2*n* 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. 24 25 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. 26 27 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. 28 29 Input 30 31 The first line of input contains the integer *n* (1 ≤ *n* ≤ 3·105) — the number of boxes. 32 33 Each of the next 2*n* 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. 34 35 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. 36 37 Output 38 39 Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. 40 41 Examples 42 43 Input 44 45 ``` 46 3 47 add 1 48 remove 49 add 2 50 add 3 51 remove 52 remove 53 ``` 54 55 Output 56 57 ``` 58 1 59 ``` 60 61 Input 62 63 ``` 64 7 65 add 3 66 add 2 67 add 1 68 remove 69 add 4 70 remove 71 remove 72 remove 73 add 6 74 add 7 75 add 5 76 remove 77 remove 78 remove 79 ``` 80 81 Output 82 83 ``` 84 2 85 ``` 86 87 Note 88 89 In the first sample, Daru should reorder the boxes after adding box 3 to the stack. 90 91 In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack.