欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CF Okabe and Boxes

程序员文章站 2023-12-25 22:51:09
...
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, andn 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 from1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe'sremove 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 numberx to the top of the stack.

It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, andn 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.

意思:给你2*n个操作,其中n个是出栈,n个是入栈,现在他想让你按照从小到大的顺序出盏,让你求出daru需要的最小排序数。

暴力明显不行,大约第九组数据就超时了。

我是用的优先队列来做的,小根堆。一个小根堆,一个盏。小根堆用来存放当前理想状态,盏用来存在当前的现实状态。当现实与理想不符合的时候我们抛去现实状态,只要理想状态,同时排序数+1。因为我们通过一次排序,就是把现实状态变成了理想状态,所以在排序数+1的情况下,我们需要清空现实栈来模拟我们的思想。

#include<bits/stdc++.h>
using namespace std;
const int M=300000+10;
int tre[2*M];
int topp;
priority_queue<int,vector<int>,greater<int> >ycq;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        topp=0;
        char str[10];
        int num;
        int tn=2*n;
        int del=0;
        int sum=0;
        for(int i=1; i<=tn; i++)
        {
            scanf("%s",str);

            if(str[0]=='a')
            {
                scanf("%d",&num);
                ycq.push(num);
                tre[++topp]=num;
            }
            else
            {

                if((!ycq.empty())&&(ycq.top()==del+1))
                {
                    if(topp&&ycq.top()!=tre[topp])
                    {
                        sum++;
                        topp=0;//抛去现实栈
                    }
                    ycq.pop();
                    if(topp>0)
                        topp--;
                    del++;
                }
            }
        }
        printf("%d\n",sum);
    }
}


上一篇:

下一篇: