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

WRF和WPS模型在Ubuntu系统上的安装与常见问题解决指南 本文详细介绍了在Ubuntu系统上安装WRF和WPS模型的完整教程,涵盖系统环境配置、依赖库安装、源代码编译以及常见问题的解决方案。重点解决了WPS编译中intmath.f文件的修改、环境变量设置和编译器兼容性等关键难点,帮助用户高效部署气象研究与预报模型。 阅读详情

题目
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 customers 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 custome​2​​ is served at window2. Customer
​3 will wait in front of window​1​​ and customer​4​​ will wait in front of window2. Customer​5​​ will wait behind the yellow line.

At 08:01, customer1 is done and customer​5​​ enters the line in front of window1 since that line seems shorter now. Customer2​​ will leave at 08:02, customer4​​ 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 (≤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

题目大意
  • n个窗口,每个窗口前可以排队m人。
  • 有k位用户需要服务,给出了每位用户服务需要占用的的minute数。
  • 银行在8点开始服务,如果有窗口还没排满就在窗口前排队,否则就在黄线外等候。如果有某一队列有一个用户走了服务完毕了,黄线外的人就进来一个。如果同时有多个窗口走了一个人,就选窗口数最小的那个窗口前去排队。
  • 要求输入q个人,以HH:mm的格式输出他们的服务结束时间。银行在17:00点停止服务,如果一个客户在17:00以及以后还没有开始服务(此处不是结束服务是开始17:00)就输出Sorry。
易错点分析

这个题最易错的地方是如何判断一个客户无法完成服务,

是看他的结束时间是否超过17:00吗??不不不不不,只要他在17:00前开始被服务,银行就必须给他服务完才结束下班。所以我们要看他的开始服务时间是不是超过17:00,而不是结束时间!!!

解题思路
  • 既然是排队,那么当然要用队列了,每个窗口前都有一个队,假如有n个窗口,那就建n个队列呗,但是因为编号都是从1开始,所以直接建n+1个队列好了
  • 使用结构体来保存顾客的信息,包括开始服务时间、服务花费时间、结束服务时间,这样我们最后只需要看他的开始服务时间是否超过17:00就可以了。
  • 字符串形式时间不好判断,题目中给出的是以分钟为单位的,那么我们直接用int保存就可以了,8:00就相当于8*60,17:00就相当于17*60,最后输出HH:mm很好办printf("%02d:%02d\n", customer[id].end_time / 60, customer[id].end_time % 60);
  • 首先银行开始服务,每个窗口前可以排m个人,那么前n*m个人可以直接排到黄线前面,所以这部分人要单独处理,后面的人才需要等前面有人走了再去排队。
  • 对于黄线后面的人,逐个分析,首先判断所有窗口前的队列中的队首元素,看那个人的服务结束时间,找到最早的那个,让那个人出队,然后他排到这条队伍后面,这就相当于实现了题目中他总是选择排到最短的队伍后面
满分代码

个人觉得我代码注释够清楚了,应该都能看懂的吧,哈哈。

#include <iostream>
#include <queue>
using namespace std;

struct Customer {
    int cost_time, start_time, end_time;
} customer[1001];

int main() {

    // n个窗口,每个窗口到黄线,最多排m个人,k个客户,有q个客户想知道他啥时候被服务结束
    int n, m, k, q;
    cin >> n >> m >> k >> q;
    // 银行开始服务时间,结束服务时间
    int open_time = 8 * 60, close_time = 17 * 60;
    // n个窗口,从1开始
    queue<Customer> windows[n + 1];

    // 读入k个人花费的时间
    for (int i = 1; i <= k; ++i)
        cin >> customer[i].cost_time;
    // 排队
    for (int i = 1; i <= k; ++i) {
        // 黄线内能排 n * m个人
        if (i <= n * m) {
            // 按顺序挨个窗口,顾客id和窗口id都从1开始
            int window = (i - 1) % n + 1;
            // 前n个人,一人一个窗口
            if (i <= n) {
                // 开始时间是银行开始服务时间
                customer[i].start_time = open_time;
                // 他的结束时间是他的开始加+他的服务时间
                customer[i].end_time = open_time + customer[i].cost_time;
            } else {
                Customer last_customer = windows[window].back();
                // 他的开始时间是上一个人的结束时间
                customer[i].start_time = last_customer.end_time;
                // 他的结束时间是他的开始加+他的服务时间
                customer[i].end_time = customer[i].start_time + customer[i].cost_time;
            }
            // 排到对应窗口
            windows[window].push(customer[i]);
        // 黄线外的人
        } else {
            // 看哪一个窗口先离开一个人,就是看每个窗口前那个队伍中第一个人的离开时间
            int window = 1;
            for (int j = 2; j <= n; ++j) {
                // 除非第一个人离开的更早,才选择这个窗口
                if (windows[j].front().end_time <
                    windows[window].front().end_time) {
                    window = j;
                }
            }
            // 这个窗口第一个人出队
            windows[window].pop();
            // 排到这个窗口
            Customer last_customer = windows[window].back();
            // 他的开始时间是上一个人的结束时间
            customer[i].start_time = last_customer.end_time;
            // 他的结束时间是他的开始加+他的服务时间
            customer[i].end_time = customer[i].start_time + customer[i].cost_time;
            //入队
            windows[window].push(customer[i]);
        }
    }
    // q个人想要查询自己什么时间能结束
    int id;
    while (q-- > 0) {
        cin >> id;
        // 开始服务时间超过了银行关门时间,无法服务
        if (customer[id].start_time >= close_time)
            cout << "Sorry" << endl;
        else
            printf("%02d:%02d\n", customer[id].end_time / 60,
                   customer[id].end_time % 60);
    }

    return 0;
}
RiscV运行zephyr和Linux的方法 riscv基金会搞了一个网站,用于介绍如何使用riscv指令集进行开发。 对于运行zephyr和linux以及Qemu有非常大的帮助。 可以作为riscv开发的相关参考。 RISC-V - Getting Started Guide — RISC-V - Getting Started Guidehttps://risc-v-getting-started-guide.readthedocs.io/en/latest/index.html RISC-V - Getting Started Gu. 阅读详情

相关推荐

MLX90640 热成像 STM32

MLX90640 热成像

zhaocundang's blog 5007

PTA 甲级 1014 Waiting in Line (30 分)(测试点4,5)

题目:1014 Waiting in Line (30 分) 题解:注意事项!!!血的教训 1.首先就是只要在17:00之前接受服务的客户,及时截止时间超过17:00,也是会被服务的,也就是说不能输出sorry 2.注意n、m的取值,如果两个等于0 ???所以这里对队列的处理最好是i<=n*m。这个错误找了好久,看我下面那个注释掉的,就是窗口前的第1位和后面m-1位拆开来处理了,会导致错误的发生 #include<bits/stdc++.h> using namespace std;

记录一些刷过的题并分享学习心得 674

基于PI双闭环解耦控制的三相SVPWM电压型逆变器(1)--数学模型

三相电压型逆变器、双PI控制、SVPWM、解耦

weixin_58670090的博客 1万+

PAT A1014:Waiting in Line

题目描述 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...

lilian的博客 333

结合qtdesigner,用pyqtgraph在界面上绘制波形图(一)

思路:先画图,然后在qtdesigner上放一个容器,给这个容器添加一个布局,然后把图当作一个Widget放入布局中。二、PyUIC把test.ui转换成test.py,再新建一个load_test.py写脚本。就这么简单,基础部分就完成了。也可以给它添加样式,我知道的样式不算多,但会继续写。一、随便拖进来一个容器控件,我拖了一个Widget。要想把波形图显示在界面上,核心思想其实很简单。三、load_test.py的代码。

learninger_lt7的博客 1803

YOLOv11涨点改进 | Neck改进篇 | YOLOv11引入 BiFPN双向特征金字塔网络,显著提升了多尺度特征融合效率

本文介绍了基于BiFPN特征融合模块优化的YOLOv11目标检测模型。BiFPN通过双向跨尺度连接和加权特征融合机制,显著提升了多尺度特征融合效率。该方法在COCO数据集上实现了55.1AP的检测精度,同时大幅降低计算成本(减少13-42倍FLOPs)。文章详细解析了BiFPN模块结构、核心代码实现方法,并提供了YOLOv11-BiFPN配置文件的修改指导,为计算机视觉领域提供了一种高效的目标检测解决方案。

Ai缝合怪、助力小伙伴高效发论文 716

1014 Waiting in Line 队列操作

题目 题目链接:1014 Waiting in Line (PAT (Advanced Level) Practice) 输入样例 2 2 7 5 1 2 6 4 3 534 2 3 4 5 6 7 输出样例 08:07 08:06 08:10 17:00 Sorry 提交结果截图 带详细注释的源代码 代码参考了博文——PTA 1014 Waiting in Line (30分) 解题思路及满分代码 #include <iostream> #include .

NOT_TODAY1的博客 572

SAP 生产订单归档简介

通常在使用了几年的SAP后,系统中会产生非常多的数据在系统中,特别是制造业的生产订单,常年的累积会存在很多的订单在系统中,这个时候就可以使用归档的功能,对生产订单进行处理。标记DLFL的前提是需要完成生产订单的差异结算,DLFL标记可以清除掉生产订单的相关需求和预留。6、在执行CO78选择删除,前面的操作相当于我们把需要归档的数据写入了数据库中,现在正式进行数据的归档删除。然后我们co03在去看生产订单,可以看到生产订单中多了删除的状态和标记的状态。我这里直接执行的程序PPARCHP1,效果其实是一样的。

我小时候很黑的博客 2285

1014 Waiting in Line

就是给出的顾客服务时间,动态模拟每个顾客结束的时间,不过需要考虑的因素很多,有很多窗口,还有线外线内等。

hlp_whale的博客 239

华为OD机试 - 单词倒序(Python/JS/C/C++ 2025 A卷 100分)

华为OD机试 2025A卷题库疯狂收录中,刷题。

学Java,找哪吒 1226

【用unity实现100个游戏之34】使用环状(车轮)碰撞器(Wheel Collider)从零实现一个汽车车辆物理控制系统,实现一个赛车游戏

本文介绍了在Unity中使用Wheel Collider组件实现3D车辆物理控制系统的详细步骤。主要内容包括: Wheel Collider参数详解:包括基础参数(质量、半径等)、悬挂弹簧系统(弹力、减震等)和摩擦力设置(前向/侧向摩擦曲线)。 准备工作流程: 导入赛车模型、赛道和树木素材 为车身添加Rigidbody和MeshCollider 创建四个Wheel Collider并调整参数(半径、悬挂距离等) 优化摩擦系数设置防止侧滑 实现车辆控制: 通过代码控制车轮前后移动和转向 提供底盘系统设置(四轮

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 2658

1014 Waiting in Line (30分)【queue队列】

1014Waiting in Line(30分) Suppose a bank hasNwindows 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 ...

胡小涛的博客 363

华为OD技术面八股文_C++_01

汇总华为OD技术面C++方向八股文真题,并提供详细解答。

qq_45776114的博客 1887

1014 Waiting in Line(30分)

当某一窗口有一个人办理完业务,黄线外的第一个人立刻补充到该窗口黄线内的队伍里(哪个队伍短去哪个,如果一样长,去编号最小的)。按照"送客->入队->迎客"的流程即可,指的注意的是为了便于计算,使用一个for循环,将时间都转化为分钟进行。最初按窗口顺序在黄线里排队,如果黄线里排满,则统一排到黄线外(只有一个队列)。K个人,N个窗口,每个窗口分为黄线内和黄线外, 每个窗口黄线内可以站M个人。

a131529的博客 242

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

PAT-ADVANCED1014——Waiting in Line

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805498207911936 题目描述: 题目翻译: 1014 排队 假设一家银行共有N个窗口开放提供服务。每个窗口前面有一条黄线...

清风阁 404

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Waiting in Line(队列Queue)

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

weixin_38902950的博客 793

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

PAT甲级 1014 Waiting in Line (30 分) 题解 全网最简单逻辑和写法

\quad这个模拟题需要用到队列,即开始的时候创建N个队列,给定时间无限长,我设定的10000分钟,保证每个人都能办理完。按照要求将人员一一入队,每过一分钟,将队首元素的time,即办理业务所需时间减1,如果time值变为0,说明业务已经办理完成,记录该人员结束的时间leaveTime。再下一分钟时检验还有哪些队列大小小于M,小于的队列在人员还有剩余的情况下入队,如此循环即可。因为设定的时间足够...

qq_40438165的博客 1986

【PAT】1014. Waiting in Line

考查点:模拟题,可以用优先队列 提交情况:第一次排队时候按列排搞错方向,第二次用优先队列push时候重复了,搞了半天,最后把优先队列单独push,最后是sorry的处理,跟题意理解有关,最后是没注意到出现队长相同的情况因为优先队列是不稳定排序的所以重载运算符的时候需要比较窗口号 思路:对于模拟题,首先要研究好样例,是怎么计算出来的,然后模拟计算过程,如果需要预处理的信息就开一个数组记录需要用到

天行健,君子以自强不息 741

1014 Waiting in Line (PAT甲级)

我的代码也可以改进一下,把window结构体的id取消掉,每次只需要循环一遍窗口就能找出最短队伍(firstLeave最小的窗口)。我的解法就是先把所有人的结束时间初始化到INF,然后算在5pm之前被服务的客户的结束时间,最后输出的时候做一下判断就可以了。

linh2006的博客 265

1014.Waiting in Line

【题意】 模拟银行排队 【思路】 直接模拟,设置n个代表窗口的队列以及1个等待队列 【注意点】 1. 17:00之前开始受理的用户即使结束时间超过了17:00,也不能输出Sorry; 2. 可能同时有多个窗口空出来; #include #include #include using namespace std; #define MAX 0x0fff

不应该放弃,直到失去的那一刻 472

【PAT(甲级)】1014 Waiting in Line(详细分析了排队原理)

【PAT(甲级)】1014 Waiting in Line 详细分析排队原理,解题思路,代码注释也非常详细

weixin_55202895的博客 595

1014 Waiting in Line (30分) 队列,银行排队

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 fro

weixin_45331809的博客 382

PAT B1014 福尔摩斯的约会 (20point(s))

大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间星期四 14:04,因为前面两字符串中第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母 D,代表星期四;第 2 对相同的字符是 E ,那是第 5 个英文字母,代...

qq_42347617的博客 723

1014 Waiting in Line (30分)解题思路

1014 Waiting in Line (30分)解题思路 原题链接 这题我觉得有点难度,细节上的东西不少,最重要的思想是要设定一个统一的时间轴timeLine。 思路 设置N个队列(每个窗口都对应一个队列),队列长度最大为M,初始化的时候将每个人的编号填入所有的队列,如果填满了N*M,就停止填充,剩下的编号作为排队线以外的顾客编号。填充方法如下图 另外,开辟一个数组cost_time[],cost_time[i]表示顾客i办理业务的耗时 然后开启循环,在所有队列的首部中找出耗时最少的顾客的编号,假设下

SingDanceRapBall的博客 384

pat甲级1014 Waiting in Line(C语言)

【代码】pat甲级1014 Waiting in Line(C语言)

qq_62438314的博客 327

1014. Waiting in Line (30)-PAT甲级真题(queue的应用)

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 t

柳婼 の blog 5517
上一篇: 红黑树的插入及调整过程(源码解读,有图有真相)
下一篇: PAT 1017 Queueing at Bank (25分) 思路详解+满分代码
wwang_dev
博客等级 码龄7年 27粉丝 76原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值