立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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

Input Specification:
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 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


题意:K个人,N个窗口,每个窗口分为黄线内和黄线外, 每个窗口黄线内可以站M个人。
最初按窗口顺序在黄线里排队,如果黄线里排满,则统一排到黄线外(只有一个队列)。
当某一窗口有一个人办理完业务,黄线外的第一个人立刻补充到该窗口黄线内的队伍里。

老老实实写模拟即可。

解题思路:由于最后需要按编号查询, 因此定义结构体,元素有编号、所需时间、完成时间等等。然后定义结构体队列, 将时间简化为分钟,一分钟一次循环, 接下来对每个队列进行操作即可。

细节:

  1. 如果K<M*N, 那么入队列的人以K为准
  2. 如果到了17点,顾客还没有被服务(注意是被服务)。 那么输出sorry

(PS:抱着Debug的决心敲得代码,反反复复调试确认了很久才提交代码,结果一遍过,太开心了!)


#include<bits/stdc++.h>
using namespace std;struct costomer{int id, n_t1, n_t2, f_t;        //编号、所需时间、实际完成时间 int h, m;      //完成的小时数,分钟数
}; bool cmp(costomer c1, costomer c2) {return c1.id < c2.id;     //id号升序排列
}int main() {int N, M, K, Q; cin >> N >> M >> K >> Q;queue<costomer>q[N];int id = 1;//处理前M*N个顾客 int Min = min(M, K);     while(1) {for(int j = 0; j < N; j++) {costomer cos;int t; cin >> t;cos.id = id++;cos.n_t1 = cos.n_t2 = t;q[j].push(cos);if(id > min(M*N, K)) goto loop;    //满足条件则退出循环 }}loop : ;int now_cos_num = max(K-M*N, 0); //剩余未处理的乘客 vector<costomer>fin;           //存放结果 int t = 0;while(1) {                        //每次循环都是一分钟 t++;int temp = 0; for(int i = 0; i < N; i++) { //每一分钟所有窗口同时-1 if(!q[i].empty()) {          //如果队列非空 q[i].front().n_t1--;if(q[i].front().n_t1 == 0) { //如果剩余处理时间为0 q[i].front().f_t = t;     //压入结果队列 costomer cos = q[i].front();q[i].pop();fin.push_back(cos);                //结果序列存放该顾客if(now_cos_num-- > 0) {           //如果还有剩余顾客 int x; cin >> x;           //输入,压入队列 cos.id = id++; cos.n_t1 = cos.n_t2 = x; cos.f_t = 0;q[i].push(cos);}}} else {    //如果空了,代表没有后续的人,temp++。 temp++; }}if(temp == N) break;  //如果temp=窗口数,则说明无人,返回。 }sort(fin.begin(), fin.end(), cmp); for(int i = 0; i < Q; i++) {int x; cin >> x; x-=1;if(fin[x].f_t-fin[x].n_t2 >= 540) cout << "Sorry\n";else printf("%02d:%02d\n", fin[x].f_t/60+8, fin[x].f_t%60);}return 0;
}

耗时:


        ——错过落日余晖,请记得还有漫天星辰。

【一遍过!!!】1014 Waiting in Line (30 分)(题意+分析)相关推荐

  1. 1014 Waiting in Line (30 分) 【未完成】【难度: 难 / 知识点: 大模拟】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805498207911936 大模拟代码有时间补

  2. PAT (Advanced Level) 1014. Waiting in Line (30)

    简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  3. 1014 Waiting in Line 队列操作

    目录 题目 输入样例 输出样例 提交结果截图 带详细注释的源代码 题目 题目链接:1014 Waiting in Line (PAT (Advanced Level) Practice) 输入样例 2 ...

  4. pat1014. Waiting in Line (30)

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

  5. PAT 1014 Waiting in Line

    2019独角兽企业重金招聘Python工程师标准>>> 1: 全错, 修改下句增加queryCnt<q 的判断: while(queryCnt < q && ...

  6. PAT (Advanced Level) 1014 Waiting in Line(模拟)

    题目链接:点击查看 题目大意:给出规则,要求模拟客户到银行办理手续的过程:为了方便描述,下面将分为等待区和服务区来称呼 银行共有n个窗口,每个窗口最多可以有m个人排队,这里我们称为服务区 若窗口排队人 ...

  7. L3-2 拼题A打卡奖励 (30 分)

    Powered by:NEFU AB-IN Link 文章目录 L3-2 拼题A打卡奖励 (30 分) 题意 思路 代码 L3-2 拼题A打卡奖励 (30 分) 题意 拼题 A 的教超搞打卡活动,指定 ...

  8. 树莓派(Raspberry Pi 3) centos7使用yum命令报错File /usr/bin/yum, line 30 except KeyboardInterrupt, e:...

    使用yum命令报错 File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid ...

  9. File “/usr/bin/yum“, line 30 及 File “/usr/libexec/urlgrabber-ext-down“, line 28

    问题: $ yum File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: SyntaxError: invalid s ...

最新文章

  1. 原理分析之一:从JDBC到Mybatis
  2. MATLAB禁用!!MATLAB教程对高校全免费!!
  3. 深度学习入门之PyTorch学习笔记
  4. linux下的bc计算器设置scale精度
  5. Java基础 —— 变量,选择,循环,数组,输入与输出等
  6. 欧氏空间内积定义_三、n维空间简介(6)矢量平移和测地线
  7. 【Java】一个List按照另一个List的数据顺序来排序
  8. activeMQ 问题
  9. 富国基金:基金公司是如何进行数据架构规划与实践的
  10. 红外遥控器解码串口输出模块结合51单片机+oled屏幕实现遥控器红外解锁( STC89C52RC)
  11. winform chart控件 滚动条
  12. C++ priority_queue 用法详解
  13. ALSA子系统(十四)------snd_pcm_drain和snd_pcm_drop
  14. 开启和关闭android移动网络
  15. c++中使用orm关系对象模型
  16. Playing Atari with Deep Reinforcement Learning-笔记
  17. python创建时间序列_python 时间序列
  18. prim算法之处女作
  19. 步进电机(四相五线为例子)步进角度和工作原理介绍
  20. 明年春天见!罗永浩发声:还完债当天就重返科技行业

热门文章

  1. 网络优化实践探索文章
  2. python wav模块获取采样率, 采样点,声道,量化位数和时间
  3. 搞懂这些SQL优化技巧,面试横着走
  4. 二叉堆详解实现优先级队列
  5. shell编程之随机数和嵌套循环
  6. 音视频技术开发周刊(第131期)
  7. LiveVideoStack线上分享第三季(十二):复杂网络下多码率视频流切换关键技术...
  8. Android Motion Stills实现AR即时运动捕捉
  9. 理解tcp关闭连接中的time_wait状态
  10. FLV封装格式介绍及解析