• PAT 1014


    1014. Waiting in Line (30)

    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.

    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

    最大的坑是:对于那些在17:00之前已经开始处理的客户必须将他们的事务处理完。

    代码

      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 typedef struct Queue{
      5     int q[11];
      6     int s,e;
      7 }Queue;
      8 void print(int);
      9 void initQueue(Queue *);
     10 int isEmpty(Queue *);
     11 int isFull(Queue *);
     12 int enterQueue(Queue *,int);
     13 int outQueue(Queue *);
     14 int readQueueBase(Queue *);
     15 
     16 int processTime[1004],remindedTime[1004],queries[1000];
     17 int finishedTime[1004];
     18 Queue queue[20];
     19 const int totalTime = 540;
     20 int main()
     21 {
     22     int N,M,K,Q;
     23     int i,j;
     24     while(scanf("%d%d%d%d",&N,&M,&K,&Q) != EOF){
     25         for(i=1;i<=K;++i){
     26             scanf("%d",&processTime[i]);
     27             remindedTime[i] = processTime[i];
     28         }
     29         for(i=0;i<Q;++i)
     30             scanf("%d",&queries[i]);
     31         memset(finishedTime,0,sizeof(finishedTime));
     32         for(i=0;i<N;++i)
     33             initQueue(&queue[i]);
     34         int yellowLineNum = 1;
     35         for(i=0;i<M;++i){
     36             for(j=0;j<N;++j){
     37                 if(yellowLineNum <= K){
     38                     enterQueue(&queue[j],yellowLineNum);
     39                     ++yellowLineNum;
     40                 }
     41                 else
     42                     break;
     43             }
     44             if(yellowLineNum > K)
     45                 break;
     46         }
     47         int nowTime = 1;
     48         int x;
     49         for(;nowTime <= totalTime;++nowTime){
     50             for(i=0;i<N;++i){
     51                 if(!isEmpty(&queue[i])){
     52                     x = readQueueBase(&queue[i]);
     53                     --remindedTime[x];
     54                     if(remindedTime[x] == 0){
     55                         finishedTime[x] = nowTime;
     56                         outQueue(&queue[i]);
     57                         if(yellowLineNum <= K){
     58                             enterQueue(&queue[i],yellowLineNum);
     59                             ++yellowLineNum;
     60                         }
     61                     }
     62                 }
     63             }
     64         }
     65         for(i=0;i<N;++i){
     66             if(!isEmpty(&queue[i])){
     67                 x = readQueueBase(&queue[i]);
     68                 if(remindedTime[x] < processTime[x])
     69                     finishedTime[x] = totalTime + remindedTime[x];
     70             }
     71         }
     72         for(i=0;i<Q;++i){
     73             if(finishedTime[queries[i]])
     74                 print(finishedTime[queries[i]]);
     75             else
     76                 printf("Sorry ");
     77         }
     78     }
     79     return 0;
     80 }
     81  
     82 void print(int t)
     83 {
     84     int h = t / 60;
     85     int s = t % 60;
     86     printf("%02d:%02d ",h+8,s);
     87 }
     88 
     89 void initQueue(Queue *Q)
     90 {
     91     (*Q).s = (*Q).e = 0;
     92 }
     93 
     94 int isEmpty(Queue *Q)
     95 {
     96     return ((*Q).s) == ((*Q).e); 
     97 }
     98 
     99 int isFull(Queue *Q)
    100 {
    101     return ((*Q).e + 1) % 11 == ((*Q).s);
    102 }
    103 
    104 int enterQueue(Queue *Q,int x)
    105 {
    106     (*Q).q[(*Q).e] = x;
    107     (*Q).e = ((*Q).e + 1) % 11;
    108     return 1;
    109 }
    110 
    111 int outQueue(Queue *Q)
    112 {
    113     int x = (*Q).q[(*Q).s];
    114     (*Q).s = ((*Q).s + 1) % 11;
    115     return x;
    116 }
    117 
    118 int readQueueBase(Queue *Q)
    119 {
    120     return (*Q).q[(*Q).s];
    121 }
  • 相关阅读:
    数据库系统原理:范式理论
    数据库系统原理:MVCC
    数据库系统原理:悲观锁、乐观锁
    数据库系统原理:封锁
    数据库系统原理:四大隔离级别
    数据库系统原理:ACID的作用以及实现原理
    计算机网络HTTP:长连接与短连接
    计算机网络HTTP:HTTP缓存
    计算机网络HTTP:状态码
    yum安装Docker
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1014.html
Copyright © 2020-2023  润新知