refresh的停车场

refresh的停车场

Time Limit: 1000MS Memory limit: 65536K

题目描述

 refresh最近发了一笔横财,开了一家停车场。由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先
进入停车场,而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道,
Del 表示停车场中出去了一辆车,Out 表示便道最前面的车辆不再等待,放弃进入停车场)。假设便道内的车辆不超过1000000.

输入

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

输出

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

示例输入

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

示例输出

18353365558
18353364208

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stackmax 10000
#define stacknum 10000
typedef long long int ElemType;

typedef struct
{
    ElemType *base;
    ElemType *top;
    int stacksize;
} SqStack;

int InitStack(SqStack &s)
{
    s.base = (ElemType*) malloc (stackmax*sizeof(ElemType));
    if (! s.base)
        exit(0);
    s.top = s.base;
    s.stacksize = stackmax;
    return 0;
}
void push(SqStack &s ,ElemType &e)
{
    if(s.top-s.base >= s.stacksize)
    {
        s.base = (ElemType *)realloc(s.base,(s.stacksize+stacknum)*sizeof(ElemType));
        if (! s.base ) exit(0);
        s.top = s.base + s.stacksize;
        s.stacksize += stacknum;
    }
    *s.top++=e;
}

int  pop(SqStack &s)
{
    if(s.top == s.base)   return 0;
    s.top--;
    return 1;
}


int StackEmpty(SqStack &s)
{
    if(s.top == s.base)
        return 1;
    else
        return 0;
}

void putstack(SqStack &s)
{
    while(s.top > s.base)
    {
        printf("%lld\n", *(s.top-1));
        s.top--;
    }
}

int GetTop(SqStack &s, ElemType &e)
{
    if(s.top == s.base)   return 0;
    e=*(s.top-1);
    return 1;
}
typedef long long int QElemType;
typedef long long int Status;
typedef struct QNode
{
    QElemType data;
    QNode *next;
} QNode, *Queueptr;
typedef struct
{
    Queueptr front;
    Queueptr rear;
} LinkQueue;

Status InitQueue (LinkQueue &q)
{
    q.front=q.rear=(Queueptr)malloc(sizeof(QNode));
    if(!q.front) exit(0);
    q.front->next=NULL;
    return 1;
}
Status EnQueue (LinkQueue &q, QElemType e)
{
    Queueptr p;
    p=(Queueptr)malloc(sizeof(QNode));
    if(!p) exit(0);
    p->data=e;
    p->next = NULL;
    q.rear->next=p;
    q.rear=p;
    return 1;
}

Status DeQueue (LinkQueue &q, QElemType &e)
{
    Queueptr p;
    if (q.front == q.rear)
        return 0;
    p = q.front->next;
    e = p->data;
    q.front->next = p->next;
    if (q.rear == p)
        q.rear = q.front;
    free (p);
    return 1;
}

Status QueueEmpty(LinkQueue &q)
{
    if(q.front==q.rear)
        return 1;
    else
        return 0;
}



int main()
{
    int n, m;
    char c[5];
    long long int num;
    SqStack s;
    LinkQueue q;
    while(~scanf("%d %d", &n, &m))
    {
        InitStack(s);
        InitQueue(q);
        int flag=1;
        while(m--)
        {
            scanf("%s", c);
            if(strcmp(c, "Add")==0)
            {
                scanf("%lld", &num);
                if((s.top-s.base)<n)
                {
                    push(s, num);
                }
                else
                {
                    EnQueue(q, num);
                }
            }
            if(strcmp(c, "Del")==0)
            {
                if(StackEmpty(s))
                    flag=0;
                else
                {
                    pop(s);
                    QElemType e;
                    DeQueue(q, e);
                    push(s, e);

                }

            }
            if(strcmp(c, "Out")==0)
            {
                if(QueueEmpty(q))
                    flag=0;
                else
                {
                    QElemType e;
                    DeQueue(q, e);
                }

            }
        }
        if(flag==0)
            printf("Error\n");
        else
            putstack(s);
    }


}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值