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

E - Okabe and Boxes

程序员文章站 2023-12-25 21:28:09
...
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.

Example
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.

**这个题就是一个模拟题,需要我们按照要求进行模拟,所以说不要说自己这不会那不会,你就用你会的就足够了。但是自己在进行模拟的时候感觉很乱,就是可能这落下一条语句,那里落下一条语句,这就是说:
1.要理解好题目要求,明白怎样的思路,还有就是条件,什么时候remove,前后关联,所以说一定弄懂这些限制条件
2.要生动形象,准确的再现,不能掉了任何一步,一步错,步步错。
这个题就是模拟一个栈的结构么,所以脑子中想像出来一个筒子,我们处理的数据只是某一个球,怎样往里面放球,怎样往外面拿,都要符合条件,这就是程序,将每一步都进行分解,细化,理解。
这个题还是有技巧的,我们这个排序,我们肯定就去想怎样排序,出来什么样的效果,但是这是消耗时间的,这就需要我们利用等效的效果,但是我们不去考虑中间的过程,只是知道他发挥了怎样的作用,再就是怎样证明他们确实是等效的,这样确实很考验自己的证明的能力。**
这个题就是在模拟一个栈的结构 ,当remove出现的时候我们看看这个数是不是应该删掉的值,是的话删掉;不是的话,就需要排序,我们要理解排序的目的是为了找到应该删掉的数把它放在最后面,然后把它删掉。下一步如果是加的话,还是加在栈顶,我们还得看这个元素是不是应该被删掉,如果是的话,那么就删掉,如果不是的话,我们就需要进行排序,我们发现这个排序完全可以忽略,因为我们总是讨论栈顶的这一个元素,并不需要管下面的元素。可能我们又想了如果remove了一次之后,接着remove 第二次,由于这个数据是合理的,没有什么幺蛾子,所以如果他说remove就说明这个应该删除的数已经在栈里面了,而我们在第一次remove的时候,我们已经排序,所以并不需要去判断,同理再来一次remove也无妨。所以讲,这种就是别怕想情况,将情况考虑到没有差错,这就是正确答案了,不要慌,慢慢的分析,合理的分析,找到题中的隐含条件。

#include<stdio.h>
#define  N  300010
int main()
{
    int n,k,temp,a[N],top=0,c=1,ans=0,flag=0;
    char str[10];
    scanf("%d",&n);
    k=2*n;
    while(k)
    {
        scanf("%s",str);
        if(str[0]=='a')
        {
            scanf("%d",&temp);
            a[top]=temp;
            top++;
        }
        else
        {
            if(top!=0)
            {
                if(a[top-1]==c)
                {
                    top--;
                    c++;
                }
                else
                {
                    ans++;
                    top=0;
                    c++;
                }
            }
            else
            {
                c++;
            }

        }
        k--;
    }
    printf("%d\n",ans);
    return 0;
}
相关标签: 模拟

上一篇:

下一篇: