数据结构实验之栈与队列十一:refresh的停车场

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

 refresh最近发了一笔横财,开了一家停车场。由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先

进入停车场,而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道,

Del 表示停车场中出去了一辆车,Out 表示便道最前面的车辆不再等待,放弃进入停车场)。假设便道内的车辆不超过1000000.

Input

 输入为多组数据,每组数据首先输入N和M(0< n,m <200000),接下来输入M条命令。

Output

 输入结束后,如果出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆,最后进入的最先输出,无车辆不输出。

Sample Input

2 6
Add 18353364208
Add 18353365550
Add 18353365558
Add 18353365559
Del
Out

Sample Output

18353365558
18353364208

 

 

 


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

char s[200001];
long long int a, stack[200001], stack1[200001];
int main(void)
{
    int n, m, i, top1, top, t, t1, flag;

    while(~scanf("%d %d", &n, &m))
    {
        top = 0;//stack的尾部
        top1 = 0;//stack1的尾部
        flag = 0;//标记是否Error的
        t = 1;//stack1中头
        t1 = 0;//stack1的总数
        for(i = 1; i <= m; i++)
        {
            scanf("%s", s);
            if(strcmp(s, "Add") == 0)
            {
                scanf("%lld", &a);

                if(top < n)
                {
                    stack[++top] = a;
                }

                else
                {
                    stack1[++top1] = a;
                    t1++;//这里必须t1++,来记住stack1的总数,不能用等号,因为t1变
                         //的时候top2可能不变呢
                }
            }

            else if(strcmp(s, "Del") == 0)
            {
                if(top > 0)
                {
                    top--;

                    if(t <= top1)
                    {
                        stack[++top] = stack1[t];
                        t++;
                        t1--;
                    }
                }

                if(top <= 0)
                {
                    flag = 1;
                }

            }

            else if(strcmp(s, "Out") == 0)
            {
                if(t1 != 0)
                {
                    top1--;
                    t1--;
                }

               else if(t1 <= 0)
                {
                    flag = 1;
                }

            }
        }

        if(flag)
        {
            printf("Error\n");
        }

        if(top != 0 && flag == 0)
        {
            for(i = top; i >= 1; i--)
            {
                printf("%lld\n", stack[i]);
            }
        }

    }

    return 0;
}

 

10-04 20:22