refresh的停车场 分类: 栈和队列 20...

refresh的停车场

TimeLimit: 1000ms Memory limit: 65536K

题目描述

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

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

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

输入

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

输出

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

示例输入

26

Add18353364208

Add18353365550

Add18353365558

Add18353365559

Del

Out

示例输出

18353365558

18353364208

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <time.h>
#include <cstdio>
#include <cctype>
#include <vector>
#include <string>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)
#define INF 0x3f3f3f3f

using namespace std;

int main()
{
    int n,m;
    int ans;
    string con;
    string num;
    while(~scanf("%d %d",&n,&m))
    {
        stack<string>park;
        queue<string>street;
        ans=0;
        bool flag=true;
        for(int i=0;i<m;i++)
        {
            cin>>con;
            if(con=="Add")
            {
                cin>>num;
                if(ans<n)
                {
                   park.push(num);
                   ans++;
                }
                else
                {
                    street.push(num);
                }
            }
            else if(con=="Del")
            {
                if(!park.empty())
                {
                    park.pop();
                    ans--;
                }
                else
                {
                    flag=false;
                }
                if(!street.empty()&&ans<n)//经入停车场
                {
                    num=street.front();
                    street.pop();
                    park.push(num);
                    ans++;
                }
            }
            else if(con=="Out")
            {
                if(!street.empty())
                {
                    street.pop();
                }
                else
                {
                    flag=false;
                }
            }
        }
        if(!flag)
        {
            cout<<"Error"<<endl;
        }
        else
        {
            while(!park.empty())
            {
                cout<<park.top()<<endl;
                park.pop();
            }
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/juechen/p/4722016.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值