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. UVALive 3713 Astronauts (2-SAT,变形)

    题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...

  2. jquery在线引用的地址

    1. 很多网站都是使用这种方式引入,客户的浏览器可能已经缓存过了 jquery.可以直接调用本地的,速度更快… 2. Google code 使用了 cdn 技术在很多地方有节点服务器,加载 jque ...

  3. sharepoint 2010 如何给文档库或自定义列表添加评论功能

    转:http://www.cfanz.cn/?c=article&a=read&id=40924 最近公司在知识库中,有一个需求,就是想要给文档添加评论功能,在sharepoint 2 ...

  4. Swift不可变数组

    Objective-C编写了2个不同的类来区分不可变数组(NSArray)和可变数组(NSMutableArray): Swift通过使用常量和变量来区分不可变数组和可变数组. 只要将数组定义为常量, ...

  5. 《深入Java虚拟机学习笔记》- 第7章 类型的生命周期/对象在JVM中的生命周期

    一.类型生命周期的开始 如图所示 初始化时机 所有Java虚拟机实现必须在每个类或接口首次主动使用时初始化: 以下几种情形符合主动使用的要求: 当创建某个类的新实例时(或者通过在字节码中执行new指令 ...

  6. ie 提示浏览器升级信息 干掉ie

    <!--[]> <div id=</a> 或以下浏览器: <a href="http://www.mozillaonline.com/"> ...

  7. C程序内存分配

    在多任务操作系统中的每一个进程都运行在一个属于它自己的内存沙盘中.这个沙盘就是虚拟地址空间(virtual address space),在32位模式下它总是一个4GB的内存地址块.这些虚拟地址通过页 ...

  8. Good Bye 2015 C - New Year and Domino

    题意:计算给定矩形面积(r1,c1),(r2,c2)内长度为2的有多少个?向右或向下计算. 思路:预处理字符.分别向右和向下处理.注意边界情况,可能算多了.用容斥原理计算长度为二的单位. #inclu ...

  9. JSF2.0 タグ一覧 (h:panelGrid) 編

    JSF の HTML (UIComponent) 系タグにはテーブルを作成するタグが2種類用意されています.これらのタグと固有機能系タグを組み合わせることでテーブルを使用した画面を作成可能です. 6. ...

  10. Java常用知识点

    1. java不支持默认参数,需要用重载来实现 2. java中要比较字符串是否相等,不能用等号,要用equals函数来比较内容 3. 尽量避免使用try catch来捕获异常,可以使用if语句判断以 ...