CF821C.Okabe and Boxes
普及/提高-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 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.
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.
Okabe 和超级黑客 Daru 正在堆叠和移除箱子。共有 n 个编号为 1 到 n 的箱子。初始时,栈中没有箱子。
Okabe 是一个控制狂,他给 Daru 下达了 2n 条命令:其中 n 条是将一个箱子压入栈顶,另外 n 条是将栈顶的箱子弹出并扔进垃圾桶。Okabe 希望 Daru 按照 1 到 n 的顺序扔掉箱子。显然,这意味着某些 Okabe 的“移除”命令可能无法执行,因为所需箱子并不在栈顶。
因此,Daru 可以选择在 Okabe 不注意时,任意时刻对栈中的箱子重新排序(即任意排列栈内现有箱子)。他可以在 Okabe 的任意两条命令之间执行该操作,但不能在重排过程中添加或移除箱子。
请告诉 Daru:为成功完成 Okabe 的所有命令,他最少需要重排栈多少次?题目保证:每个箱子均在其被要求移除之前已被加入栈中。
输入格式
The first line of input contains the integer n (1 ≤ n ≤ 3·105) — the number of boxes.
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.
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.
输入的第一行包含一个整数 n(1≤n≤3⋅105)—— 表示箱子的数量。
接下来的 2n 行输入,每行以字符串 "add" 或 "remove" 开头。若某行以 "add" 开头,则其后跟一个整数 x(1≤x≤n),表示 Daru 应将编号为 x 的箱子添加到栈顶。
保证恰好有 n 行包含 "add" 操作,所有被添加的箱子编号互不相同,且另有 n 行包含 "remove" 操作。还保证每个箱子在被要求移除之前一定已被添加过。
输出格式
Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.
输出达鲁为成功完成冈部的所有指令所需重新排列箱子的最少次数。
输入输出样例
输入#1
3 add 1 remove add 2 add 3 remove remove
输出#1
1
输入#2
7 add 3 add 2 add 1 remove add 4 remove remove remove add 6 add 7 add 5 remove remove remove
输出#2
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.
在第一个样例中,达鲁应在将箱子 3 加入栈后重新排列箱子。
在第二个样例中,达鲁应在将箱子 4 和箱子 7 加入栈后重新排列箱子。
输入解题思路,AI测评打分。不知道怎么写?