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 (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, customer​1​​ is served at window​1​​ while customer​2​​ is served at window​2​​. Customer​3​​ will wait in front of window​1​​ and customer​4​​ will wait in front of window​2​​. Customer​5​​ will wait behind the yellow line.

At 08:01, customer​1​​ is done and customer​5​​ enters the line in front of window​1​​ since that line seems shorter now. Customer​2​​ will leave at 08:02, customer​4​​at 08:06, customer​3​​ at 08:07, and finally customer​5​​ at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (≤, number of windows), M (≤, the maximum capacity of each line inside the yellow line), K (≤, number of customers), and Q (≤, 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 Specification:

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

题意分析:

(1)本题模拟银行排队,逻辑上不难,操作起来有点麻烦。银行来了K个客户,银行有N个窗口,每个窗口前有M个位置排队,客户选择最短的队伍排,根据每个客户的序号和服务时间来确定最后客户离开银行的时间。

(2)属于队列的问题,但实际上可以将问题简化:因为每个客户的服务时间是固定的,假设前N*M个客户达到之后排好位置后,他们的离开时间也就确定好了,即每个窗口本次服务结束的时间也就确定了,比如第一个人的服务时间是5min,则本次窗口的服务结束时间点是8:05,这样我们就可以比较这N个窗口当前服务结束的时间点,谁最早结束,谁的队伍最短,在线外等候的第N*M+1个客户选择这个队伍排,以此类推。

(3)每个客户离开的时间等于他前面一个人的离开时间加上他自己的服务时间,于是定义每个队列中的值是前一个值加上这个人的服务时间

(4)为了计算方便,将时间的基点设为0,算出的结果是在银行停留的时间(以分钟计算),最后再换算成小时+8

可能坑点:

(1)这是个大深坑,必须要保证当一个人等待时间大于或等于540分钟的时候,就不能服务了,而不是等待时间加上他的服务时间,言外之意就是,一旦这个人在540分钟之内获得服务,那么无论这个人的服务时间有多长,也要为他服务完,这也是符合实际的。

#include<bits/stdc++.h>
using namespace std;
queue<int>q[];
int a[];//服务时间
int ct[];//还需多少时间
int t[];//服务结束的时间
int N,M,K,Q;
void clear(queue<int> &q)
{
queue<int> empty;
swap(empty, q);
}
int main(){
cin>>N>>M>>K>>Q;
for(int i=;i<=N+;i++){
clear(q[i]);
}
for(int i=;i<=K;i++)
{
cin>>a[i];
ct[i]=a[i];
t[i]=;
}
//先把他们再队伍里放好
int p=min(N*M,K);
for(int i=;i<=p;i++)//这里出现了错误,后来知道要选择最小的
{
int x=i%N;
if(x==) x=N;
q[x].push(i);
}
if(K>=N*M+)
{
for(int i=N*M+;i<=K;i++){
q[N+].push(i);
}
}
//先标记最前面一排的人的时间
int T=;
while()
{
//每次挑出队列最前面的人群中的最少时间
int min_t=;
int is_all_empty=;
for(int i=;i<=N;i++)
{
if(!q[i].empty())
{
min_t=min(min_t,ct[q[i].front()]);
is_all_empty=;
}
}
if(is_all_empty==){
break;
}
T+=min_t;//从开始到现在过去了T分钟 //那么每个人减去这个时间,并把下一个人排进队伍
for(int i=;i<=N;i++)
{
//cout<<"遍历第"<<i<<"个窗口"<<endl;
if(!q[i].empty())//要先判断,否则会有段错误,这里错过一次
{ ct[q[i].front()]-=min_t;
if(ct[q[i].front()]==){
//cout<<i<<"窗口完成 "<<q[i].front()<<" 时间为"<<T<<endl;
//完成了
t[q[i].front()]=T; //设置他们结束的时间
q[i].pop();
//新的人可以入队啦
if(!q[N+].empty())
{
int x = q[N+].front();
q[N+].pop();
q[i].push(x);
//cout<<x<<"加入到 "<<i<<" 窗口"<<endl;
}
}
}
}
//大坑!!!特殊处理
if(T>=){
//仍要坚持把当前再服务的人服务完
for(int i=;i<=N;i++)
{
if(!q[i].empty())
{
int x=q[i].front();
if(ct[x]!=a[x])//处理到一半的继续处理
{
t[x]=T+ct[x];
}
}
}
break;
}
}
//输出时间
for(int i=;i<=Q;i++)
{
int x;
cin>>x;
if(t[x]==)
{
cout<<"Sorry"<<endl;
}
else
{
int h=t[x]/;
int m=t[x]-h*;
h+=;
if(h<)
{
cout<<'';
}
cout<<h<<":";
if(m<)
{
cout<<'';
}
cout<<m<<endl;
} }
return ;
}
 

PAT 甲级 1014 Waiting in Line (30 分)(queue的使用,模拟题,有个大坑)的更多相关文章

  1. PAT甲级1014. Waiting in Line

    PAT甲级1014. Waiting in Line 题意: 假设银行有N个窗口可以开放服务.窗前有一条黄线,将等候区分为两部分.客户要排队的规则是: 每个窗口前面的黄线内的空间足以包含与M个客户的一 ...

  2. 1014 Waiting in Line (30分)

    1014 Waiting in Line (30分)   Suppose a bank has N windows open for service. There is a yellow line i ...

  3. 【PAT甲级】1014 Waiting in Line (30 分)(队列维护)

    题面: 输入四个正整数N,M,K,Q(N<=20,M<=10,K,Q<=1000),N为银行窗口数量,M为黄线内最大人数,K为需要服务的人数,Q为查询次数.输入K个正整数,分别代表每 ...

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

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

  5. PAT A 1014. Waiting in Line (30)【队列模拟】

    题目:https://www.patest.cn/contests/pat-a-practise/1014 思路: 直接模拟类的题. 线内的各个窗口各为一个队,线外的为一个,按时间模拟出队.入队. 注 ...

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

  7. PTA 1014 Waiting in Line (30分) 解题思路及满分代码

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

  8. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  9. PAT甲级——1131 Subway Map (30 分)

    可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30  ...

随机推荐

  1. Data truncation: Incorrect datetime value: 'May 15, 2019 4:15:37 PM

    因为系统在windows下测试过是正常的 windows下的jdk+ windows下安装的mysql 全部cases通过 linux下的jdk + windows下安装的mysql 新增和更新,影响 ...

  2. linq学习(二)

    百度搜索:C# linq查询新对象 直接从list中查出一个新对象集合. 文章:https://blog.csdn.net/lym940928/article/details/80278783 fro ...

  3. SEO 统计算法

    1)简单粗暴型的,这里不用去管浏览器的user-agent,不管cookie等信息,每产生一次PV,就直接计数,优点:简单,缺点:可能不真实,也可能有刷量数据 2) 稍微细腻点的统计,会区分新老用户, ...

  4. gitlab-ce白名单设置杜绝并发数过大引起的封ip故障

    gitlab-ce 7.9安装手札以及上篇文章的问题解决 鸣谢 感谢ruby大神===>章鱼的一路指点,才能拨开迷雾见云天! 章鱼大人: 国内Ansible部落原创翻译之一! 资深运维! ROR ...

  5. MVVM框架(Vue)

    问题: 一:说一下使用 JQuery和使用框架的区别? 二: 说一下对 MVVM的理解 三: Vue中如何实现响应式 四: vue中如何解析模板 五:vue整个实现流程 1. 说一下使用 JQuery ...

  6. Java锁--非公平锁

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3496651.html 参考代码 下面给出Java1.7.0_40版本中,ReentrantLock和AQ ...

  7. BZOJ 3903 反垄断 (最大流推的结论题)

    题目 中文题目,不解释: BZOJ传送门 分析 这道题BZOJ上也只有几个人过-奇怪了 下面是正解 原问题为一个二分图边染色问题.首先考虑最好情况.最理想情况的分配为:设一个点xxx的度为dgr(x) ...

  8. Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)

    题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...

  9. SIGAI深度学习第八集 卷积神经网络2

    讲授Lenet.Alexnet.VGGNet.GoogLeNet等经典的卷积神经网络.Inception模块.小尺度卷积核.1x1卷积核.使用反卷积实现卷积层可视化等. 大纲: LeNet网络 Ale ...

  10. nc命令用法举

    什么是nc nc是netcat的简写,有着网络界的瑞士军刀美誉.因为它短小精悍.功能实用,被设计为一个简单.可靠的网络工具 nc的作用 (1)实现任意TCP/UDP端口的侦听,nc可以作为server ...