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[];
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的更多相关文章
- PAT 1014 福尔摩斯的约会 (20)(代码+思路)
1014 福尔摩斯的约会 (20)(20 分) 大侦探福尔摩斯接到一张奇怪的字条:"我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfd ...
- PAT——1014. 福尔摩斯的约会
大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”.大侦探很快就明白了,字条 ...
- PAT 1014 Waiting in Line (模拟)
1014. Waiting in Line (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 1014. 福尔摩斯的约会 (20)
大侦探福尔摩斯接到一张奇怪的字条:"我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm".大侦 ...
- 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 ...
- pat 1014 1017 排队类问题
1.用循环模拟时间 2.采用结构体模拟客户和窗口对象 3.合理处理边界,去除无用信息 4.使用自带排序sort()结合自定义功能函数compare()实现排序
- PAT 1014 福尔摩斯的约会
https://pintia.cn/problem-sets/994805260223102976/problems/994805308755394560 大侦探福尔摩斯接到一张奇怪的字条:“我们约会 ...
- 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 ...
- PAT 1014 Waiting in Line (30分) 一个简单的思路
这题写了有一点时间,最开始想着优化一下时间,用优先队列去做,但是发现有锅,因为忽略了队的长度. 然后思考过后,觉得用时间线来模拟最好做,先把窗口前的队列填满,这样保证了队列的长度是统一的,这样的话如果 ...
随机推荐
- ODAC访问oracle时,提示:由于以前的函数求值超时,函数求值被禁用,必须继续执行才能正常返回
这是因为调试时会自动对Local/Watch等窗口里面(或鼠标停留所在)的变量求值,为了防止用户写的程序错误(比如死循环),系统有一个超时限制,如果某个属性的get中做了很复杂的操作(而不是简单地返回 ...
- (Android Studio)ActionBar's Theme/Style [ActionBar主题风格修改]
(1)默认theme代码如下: 运行结果: 视觉效果:ActionBar为Dark,背景为Light. (2)将theme改为Light: 运行结果: 视觉效果:ActionBar和背景都为Light ...
- CRC(Cyclic Redundancy Check)循环冗余校验码与海明码的计算题
(17)采用CRC进行差错校验,生成多项式为G(X)=X4+X+1,信息码字为10111,则计算出的CRC校验码是 (17) .A.0000 B.0100 C.0010 D.1100试题 ...
- ADB 远程无线调试
由于自己购买的x4412 Android开发板存在一个问题,是无法同时链接USB线,和插入无线网卡.只能使用其中一个功能 需要复现一个DRM在线下载的功能,同时需要抓起一些日志信息,此处就想到了使用 ...
- 笔记:1.css样式,最前边加 @charset "utf-8";是为什么2.js判断各种浏览器的方法
表明CSS文件的页面编码为UTF-8..如果这个CSS的文件编码也是UTF-8的话..那么在浏览器中看到的CSS文件的页面中中文的注释或者中文字体就可以正确显示为中文,如果CSS的文件编码和页面不一致 ...
- HDU4614 Vases and Flowers
http://acm.hdu.edu.cn/showproblem.php?pid=4614 HDU 4614 Vases and Flowers (2013多校第二场线段树) // #pragma ...
- mysql修改字段的语句写法
http://www.111cn.net/database/mysql/50678.htm 下面为您介绍的sql语句都是mysql修改字段操作中的一些常用语句,如果您是一个刚刚接触mysql数据库的新 ...
- HDFS体系结构:(Distributed File System)
分布式系统的大概图 服务器越来越多,客户端对服务器的管理就会越来越复杂,客户端如果是我们用户,就要去记住大量的ip. 对用户而言访问透明的就是分布式文件系统. 分布式文件系统最大的特点:数据存储在多台 ...
- FTP被动模式连接及超时问题解决
问题: 1.FTPClient.listFiles()或者FTPClient.retrieveFile()方法时,就停止在那里,什么反应都没有,出现假死状态. 2.连接FTP服务器,长时间进行数据操作 ...
- Android实例-拍摄和分享照片、分享文本(XE8+小米2)
结果: 1.分享文本不好使,原因不明.有大神了解的,请M我,在此十分感谢. 2.如果想支持图片编辑,将Action事件的Editable改为True. 相关资料: 官网地址:http://docwik ...