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[];
  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[],remindedTime[],queries[];
 17 int finishedTime[];
 18 Queue queue[];
 19 const int totalTime = ;
 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=;i<=K;++i){
 26             scanf("%d",&processTime[i]);
 27             remindedTime[i] = processTime[i];
 28         }
 29         for(i=;i<Q;++i)
 30             scanf("%d",&queries[i]);
 31         memset(finishedTime,,sizeof(finishedTime));
 32         for(i=;i<N;++i)
 33             initQueue(&queue[i]);
 34         int yellowLineNum = ;
 35         for(i=;i<M;++i){
 36             for(j=;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 = ;
 48         int x;
 49         for(;nowTime <= totalTime;++nowTime){
 50             for(i=;i<N;++i){
 51                 if(!isEmpty(&queue[i])){
 52                     x = readQueueBase(&queue[i]);
 53                     --remindedTime[x];
 54                     if(remindedTime[x] == ){
 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=;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=;i<Q;++i){
 73             if(finishedTime[queries[i]])
 74                 print(finishedTime[queries[i]]);
 75             else
 76                 printf("Sorry\n");
 77         }
 78     }
 79     return ;
 80 }
 81  
 82 void print(int t)
 83 {
 84     int h = t / ;
 85     int s = t % ;
 86     printf("%02d:%02d\n",h+,s);
 87 }
 88 
 89 void initQueue(Queue *Q)
 90 {
 91     (*Q).s = (*Q).e = ;
 92 }
 93 
 94 int isEmpty(Queue *Q)
 95 {
 96     return ((*Q).s) == ((*Q).e); 
 97 }
 98 
 99 int isFull(Queue *Q)
 {
     return ((*Q).e + ) %  == ((*Q).s);
 }
 
 int enterQueue(Queue *Q,int x)
 {
     (*Q).q[(*Q).e] = x;
     (*Q).e = ((*Q).e + ) % ;
     return ;
 }
 
 int outQueue(Queue *Q)
 {
     int x = (*Q).q[(*Q).s];
     (*Q).s = ((*Q).s + ) % ;
     return x;
 }
 
 int readQueueBase(Queue *Q)
 {
     return (*Q).q[(*Q).s];
 }

PAT 1014的更多相关文章

  1. PAT 1014 福尔摩斯的约会 (20)(代码+思路)

    1014 福尔摩斯的约会 (20)(20 分) 大侦探福尔摩斯接到一张奇怪的字条:"我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfd ...

  2. PAT——1014. 福尔摩斯的约会

    大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”.大侦探很快就明白了,字条 ...

  3. PAT 1014 Waiting in Line (模拟)

    1014. Waiting in Line (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  4. PAT 1014. 福尔摩斯的约会 (20)

    大侦探福尔摩斯接到一张奇怪的字条:"我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm".大侦 ...

  5. PAT 1014. Waiting in Line

    Suppose a bank has N windows open for service.  There is a yellow line in front of the windows which ...

  6. pat 1014 1017 排队类问题

    1.用循环模拟时间 2.采用结构体模拟客户和窗口对象 3.合理处理边界,去除无用信息 4.使用自带排序sort()结合自定义功能函数compare()实现排序

  7. PAT 1014 福尔摩斯的约会

    https://pintia.cn/problem-sets/994805260223102976/problems/994805308755394560 大侦探福尔摩斯接到一张奇怪的字条:“我们约会 ...

  8. PAT 1014 Waiting in Line (模拟)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  9. PAT 1014 Waiting in Line (30分) 一个简单的思路

    这题写了有一点时间,最开始想着优化一下时间,用优先队列去做,但是发现有锅,因为忽略了队的长度. 然后思考过后,觉得用时间线来模拟最好做,先把窗口前的队列填满,这样保证了队列的长度是统一的,这样的话如果 ...

随机推荐

  1. Android UI设计系统-android selector 开始自定义样式

    Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:a ...

  2. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  3. UESTC 250 windy数(数位DP)

    题意:题意:求区间[A,B]之间的,不含前导0,且相邻两数位之间相差至少为2的正整数有多少个. 分析:dp[i][j]表示,长度为i.以j为结尾的表示的个数,再加一个前导0判断即可 #include ...

  4. bug描述注意点

    一个好的错误跟踪系统包括了错误的必要信息,如果做得不好,会造成迷惑,并误导读者.好的故障描述应该包括十个基本部分:标题.项目.所属模块.优先级.重要性.异常等级.可重复性.现象.操作过程和附件. ①标 ...

  5. 烧写u_boot系统和linux系统

    今天下午准备烧写一下u_boot还有linux系统,因为是笔记本电脑,吐槽一下,笔记本电脑的usb转串口不是怎么稳定,dnw下对应的驱动也不怎么好用,导致在笔记本电脑上烧写系统的成功率比较低,本来三点 ...

  6. js中的new关键字都干了些什么?

    new 操作符 在有上面的基础概念的介绍之后,在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在javascript中,我们将这类方式成为Pseudoclassic ...

  7. C#中克隆随机数的三种方法(为什么我想到了茴字的四种写法

    C#中的Random类可以用来产生伪随机数(正确的废话 伪随机数的一个性质就是给定一个初始种子,那么产生的随机数列都是相同的,这个性质非常适合用在不同环境之间同步逻辑的场景. 最近有一个要克隆当前随机 ...

  8. highcharts图表的图例legend怎么改变显示位置

    一.将图例Legend放于图表右侧1.设置chart的marginRight属性值:chart: { marginRight: 120}2.设置legend图例属性值如下 legend: { alig ...

  9. BNUOJ-26475 Cookie Selection 堆,线段树等

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26475 题意:每次输入一个操作,如果是数字,那么放入一个容器中,如果是#号,取出当前容器中 ...

  10. java 通过网络 ntp 获取网络时间

    需要 commons-net-3.4.jar 库. import org.apache.commons.net.ntp.NTPUDPClient; import org.apache.commons. ...