1014. Waiting in Line (30)

喵的,看到各位大神说,这题很简单呀,用队列模拟就好。。。。可是我想了很久呀。。。

还有已接的业务可以做到17:59这个大坑呀。

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
Customer[i] will take T[i] minutes to have his/her transaction processed.
The first N customers are assumed to be served at 8:00am.
Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 customers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line.

At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.

Input

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

Output

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output "Sorry" instead.

Sample Input
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output
08:07
08:06
08:10
17:00
Sorry
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define INF 0x3f3f3f

typedef int ElementType;
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode
{
    ElementType *Data;
    Position front, rear;
    int Maxsize;
    int curSize; /**<当前数量 */
};
typedef PtrToQNode Queue;

int smallestQ(Queue q[], int num, int endTime[], int time);
int smallestQ(Queue q[], int num, int endTime[], int time)
{
    int i;
    int minQ = 0;
    int value = -1;
    for(i=0; i<num; i++)
    {
    //printf("window[%d]:%d VS window[%d]:%d\n", minQ, q[minQ]->curSize , i, q[i]->curSize);
        if(endTime[i]<=time)
        {
            if(q[minQ]->curSize >= q[i]->curSize)
            {
                minQ = i;
                value = q[minQ]->curSize;
            }
        }

    }
    if((q[minQ]->curSize == q[minQ]->Maxsize) || (value == -1))
    {
        return -INF;
    }
    else
    {
        return minQ;
    }
}


Queue creatQueue(int Maxsize);
Queue creatQueue(int Maxsize)
{
    Queue q = (Queue) malloc(sizeof(struct QNode));
    q->Data = (ElementType *)malloc(Maxsize*sizeof(ElementType));
    q->Maxsize = Maxsize;
    q->front = q->rear = 0;
    q->curSize = 0;
    return q;
}

bool IsFull(Queue q);
bool IsFull(Queue q)
{
    //return ((q->rear+1)%q->Maxsize == q->front);
    return (q->curSize == q->Maxsize);
}

bool AddQ(Queue q, ElementType x);
bool AddQ(Queue q, ElementType x)
{
    if(IsFull(q))
    {
        printf("Queue is full\n");
        return false;
    }
    else
    {
        q->rear = (q->rear+1)%q->Maxsize;
        q->Data[q->rear] = x;
        q->curSize++;
        return true;
    }
}

bool IsEmpty(Queue q);
bool IsEmpty(Queue q)
{
    return (q->curSize == 0);
}

ElementType DeleteQ(Queue q);
ElementType DeleteQ(Queue q)
{
    if(IsEmpty(q))
    {
        printf("Queue is empty\n");
        return -INF;
    }
    else
    {
        q->curSize--;
        q->front = (q->front+1)%q->Maxsize;
        return q->Data[q->front];
    }
}

int main()
{
    int wdwNum, queCap, CtrNum, qryNum;
    int i;

    scanf("%d %d %d %d", &wdwNum, &queCap, &CtrNum, &qryNum);

    int guest[CtrNum][2]; /**< 一级下标为每个客人的编号,二级下标中,0为业务处理时间,1为等待时间 */

    Queue window[wdwNum]; /**< 每个窗口开一个队列 */
    /**< 初始化每个窗口 */
    for(i=0; i<wdwNum; i++)
    {
        window[i] = creatQueue(queCap);
    }

    /**< 黄线外的队列 */
    Queue OutLine = creatQueue(CtrNum+1);

    /**< 录入每个客人业务处理时间 */
    for(i=0; i<CtrNum; i++)
    {
        AddQ(OutLine, i);
        int prcTime;
        scanf("%d", &prcTime);
        guest[i][0] = prcTime;
        //printf("Guest[%d] need %d\n", DeleteQ(OutLine), guest[i][0]);
    }

    int time = 0;
    int endTime[wdwNum];
    for(i=0; i<wdwNum; i++)
    {
        endTime[i] = 0;
    }

    int loop = 0;
    bool isOver = false;
    while(1)
    { 
        /**< 黄线外的客人找到最少人的窗口入队 */
        int idleWindow = 0;
        idleWindow = smallestQ(window, wdwNum, endTime, time);

        while(idleWindow!=-INF && !IsEmpty(OutLine))
        {
            int enQGuest = DeleteQ(OutLine);
            AddQ(window[idleWindow], enQGuest);
            idleWindow = smallestQ(window, wdwNum, endTime, time);

        }

        /**< 窗口开始处理业务 */
        for(i=0; i<wdwNum; i++)
        {
            if(!IsEmpty(window[i]))
            {
                int curGuest;
                if(endTime[i] == time && time<540)
                {
                    curGuest = DeleteQ(window[i]);
                    endTime[i] += guest[curGuest][0];
                    guest[curGuest][1] = endTime[i];
                }
                else if(time>=540) /**< 最大坑点,17点前接的业务是可以最多服务至17:59的 */
                {/**< 所以当时间到了540的客人,仍然要弹出,但是服务时间就加个540,当无限 */
                    curGuest = DeleteQ(window[i]);
                    endTime[i] += 540;
                    guest[curGuest][1] = endTime[i];
                }
            }

        }

        if(IsEmpty(OutLine))
        {
            for(i=0; i<wdwNum; i++)
            {
                if(!IsEmpty(window[i]))
                {
                    isOver = false;
                }
            }
            if(isOver)
            {
                break;
            }
        }
    isOver = true;
    time++;
    }

    for(i=0; i<qryNum; i++)
    {
        if(i!=0)
        {
            printf("\n");
        }

        int qryGuest;
        scanf("%d", &qryGuest);
        int fshTime = guest[qryGuest-1][1];

        int hh, mm;
        hh = 8;
        hh += fshTime/60;
        mm = fshTime%60;
        if(hh>17)
        {
            printf("Sorry");
        }
        else
        {
            printf("%02d:%02d", hh, mm);
        }

    }

    system("pause");
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值