• 九度OJ 1326:Waiting in Line(排队) (模拟)


    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:220

    解决:64

    题目描述:

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

    输入:

    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.

    输出:

    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.

    样例输入:
    2 2 7 5
    1 2 6 4 3 534 2
    3 4 5 6 7
    样例输出:
    08:07
    08:06
    08:10
    17:00
    Sorry

    思路:

    模拟题,排队题当然数据结构用队列比较好。

    用C语言太苦逼了,还要自己写一个队列,还是C++比较好,以后要用C++了。

    需要注意的是:即使是17点之前开始办业务,17点之后仍然没有办完就是sorry。


    代码:

    #include <stdio.h>
    #include <stdlib.h> 
     
    #define N 21
    #define M 11
    #define K 1001
     
    typedef struct node1 {
        int proc;
        int begin;
    } Cust;
         
    typedef struct node2 {
        Cust *cust[M];
        int front;
        int rear;
    } Queue;
     
    int isEmpty(Queue *q)
    {
        return (q->front == q->rear);
    }   
         
    int isFull(Queue *q)
    {   
        return (q->front == (q->rear+1)%M);
    }
         
    int countQueue(Queue *q)
    {       
        return (q->rear + M - q->front) % M;
    }           
                 
    void push(Queue *q, Cust *val)
    {       
        q->cust[q->rear] = val;
        q->rear = (q->rear+1)%M;
    }           
                 
    Cust *pop(Queue *q)
    {       
        int front = q->front;
        q->front = (q->front+1)%M;
        return q->cust[front];
    }
     
    Cust *top(Queue *q)
    {
        return q->cust[q->front];
    }
     
    int main(void)
    {
        int n, m, k, q, i, j;
        Queue *queue[N];
        Cust *cust[K];
        int query[K];
        int maxTime = (17-8)*60;
     
        while (scanf("%d%d%d%d", &n, &m, &k, &q) != EOF)
        {
            for (i=0; i<n; i++)
            {
                queue[i] = (Queue *)malloc(sizeof(Queue));
                queue[i]->front = queue[i]->rear = 0;
            }
            for (i=0; i<k; i++)
            {
                cust[i] = (Cust *)malloc(sizeof(Cust));
                scanf("%d", &cust[i]->proc);
                cust[i]->begin = maxTime+1;
            }
            for (i=0; i<q; i++)
                scanf("%d", &query[i]);
     
            // init state
            int inQueue = 0;
            int outQueue = k;
            for (i=0; i<m; i++)
            {
                for (j=0; j<n; j++)
                {
                    inQueue = i*n + j;
                    if (inQueue >= k)
                        break;
                    if (i == 0)
                        cust[inQueue]->begin = 0;
                    push(queue[j], cust[inQueue]);
                }
                if (inQueue >= k)
                    break;
            }
            inQueue = (m*n <= k) ? (inQueue+1) : inQueue;
            outQueue = k - inQueue;
            //printf("inQueue=%d, outQueue=%d, k=%d
    ", inQueue, outQueue, k);
         
            // simulate the whole process
            int time;
            for (time=0; time<=maxTime; time++)
            {
                for (i=0; i<n; i++)
                {
                    if (isEmpty(queue[i]))
                        break;
                    Cust *c = top(queue[i]);
                    if (c->begin + c->proc == time)
                    {
                        pop(queue[i]);
                        if (outQueue > 0)
                        { 
                            push(queue[i], cust[k-outQueue]);
                            outQueue --;
                        }
                        if (!isEmpty(queue[i]))
                        {
                            top(queue[i])->begin = time;
                            //printf("i=%d, time=%d
    ", i, time);
                        } 
                    }
                }
                if (isEmpty(queue[0]))
                    break;
            }   
                     
            for (i=0; i<q; i++) 
            {
                int id = query[i] - 1;
                int endTime = cust[id]->begin + cust[id]->proc;
                if (endTime > maxTime)
                    printf("Sorry
    ");
                else
                    printf("%02d:%02d
    ", 8 + endTime/60, endTime%60);
            }
        }
     
        return 0;
    }
    /**************************************************************
        Problem: 1326
        User: liangrx06
        Language: C
        Result: Accepted
        Time:10 ms
        Memory:916 kb
    ****************************************************************/


    编程算法爱好者。
  • 相关阅读:
    安卓源码版本清单(转载待查)
    【校招VIP 前端】电影详情模块的开发文档设计实战
    【校招VIP】“推推”项目课程Java:SpringBoot demo的说明、入门和下载
    【校招VIP java】产品原型功能点的理解
    【校招VIP】项目计划“推推”:小说一更新就通知(2022产品)
    PMP7.项目成本管理7.1规划成本管理
    软考信息安全操作系统安全保护
    PMP8.项目质量管理8.3控制质量
    PMP7.项目成本管理7.4控制成本
    PMP8.项目质量管理8.2管理质量
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083801.html
Copyright © 2020-2023  润新知