【PAT1014】 Waiting in Line (30) queue模拟排队

2024-04-05 06:18

本文主要是介绍【PAT1014】 Waiting in Line (30) queue模拟排队,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1014. Waiting in Line (30)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
题意:

银行分多窗口进行排队进行业务办理,求客户的结束时间。

分析:

可以纯粹使用queue进行过程模拟。

代码:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
using namespace std;//此代码使用前,需删除下面两行+后面的system("PAUSE")
ifstream fin("in.txt");
#define cin finconst int closeTime = 17;
const int closeTimeInt = 17*60;struct Window{int hour;			//窗口的每次服务的开始时间int minute;int winEnd;			//每次服务完后的时间整型queue<int> que;		//窗口队列Window(){hour=8;minute=0;}
}win[20];queue<int> waiting;		//黄线外等待队列
int cusTime[1001]={0};	//每个客户的业务办理时间struct FinishTime{		//每个客户的结束时间int hour;int minute;
}finishTime[1001];int main()
{int n,m,k,q;cin>>n>>m>>k>>q;int i;for(i=1;i<k+1;i++){cin>>cusTime[i];}int insideNum,waitNum;if(n*m >= k){insideNum = k;waitNum = 0;}else{insideNum = n*m;waitNum = k-insideNum;}for(i=0;i<insideNum;i++){			//根据先后把人安排到黄线内的对应窗口win[i%n].que.push(i+1);}int j,earlyFlag,cust;int endTime = 0x7fffffff;if(waitNum){for(i=insideNum;i<k;i++){		//把剩下的人放入等待队列waiting.push(i+1);}for(j=0;j<n;j++){				//预处理每个窗口的第一个客户,找出最早结束的一个cust = win[j].que.front();finishTime[cust].hour = win[j].hour + (win[j].minute+cusTime[cust])/60;finishTime[cust].minute = (win[j].minute+cusTime[cust])%60;win[j].hour = finishTime[cust].hour;win[j].minute = finishTime[cust].minute;win[j].winEnd = finishTime[cust].hour*100 + finishTime[cust].minute;if(win[j].winEnd < endTime){endTime = win[j].winEnd;earlyFlag = j;}}do{win[earlyFlag].que.pop();//循环执行{最早结束的一个出列;等待队列补上;找当前每窗口第一个客户中最早结束的一个;}直到等待队列为空;此时所有人都在窗口队列里了。win[earlyFlag].que.push(waiting.front());waiting.pop();cust = win[earlyFlag].que.front();finishTime[cust].hour = win[earlyFlag].hour + (win[earlyFlag].minute+cusTime[cust])/60;finishTime[cust].minute = (win[earlyFlag].minute+cusTime[cust])%60;win[earlyFlag].hour = finishTime[cust].hour;win[earlyFlag].minute = finishTime[cust].minute;win[earlyFlag].winEnd = finishTime[cust].hour*100 + finishTime[cust].minute;endTime = 0x7fffffff;for(j=0;j<n;j++){if(win[j].winEnd < endTime){endTime = win[j].winEnd;earlyFlag = j;}}}while(!waiting.empty());}for(i=0;i<n;i++){if(waitNum)win[i].que.pop();		//如果之前有等的,那窗口的第一个客户时间已经计算过,直接出列即可while(!win[i].que.empty()){			//对每个窗口队列的人依次进行时间计算cust = win[i].que.front();finishTime[cust].hour = win[i].hour + (win[i].minute+cusTime[cust])/60;finishTime[cust].minute = (win[i].minute+cusTime[cust])%60;win[i].hour = finishTime[cust].hour;win[i].minute = finishTime[cust].minute;win[i].que.pop();}}int queryID;for(i=0;i<q;i++){cin>>queryID;if(finishTime[queryID].hour >= closeTime){if(finishTime[queryID].hour*60 + finishTime[queryID].minute - cusTime[queryID] >= closeTimeInt){		//算开始时间是否在关门点后cout<<"Sorry"<<endl;continue;}}cout<<setw(2)<<setfill('0')<<finishTime[queryID].hour<<":"<<setw(2)<<setfill('0')<<finishTime[queryID].minute<<endl;}system( "PAUSE");return 0;
}

代码看起来有些臃肿,但竟然一次就AC了。

纯粹的模拟算法实现,看起来很容易理解。

更简练的实现可以参见 http://blog.csdn.net/huajh7/article/details/7918144

这篇关于【PAT1014】 Waiting in Line (30) queue模拟排队的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/877828

相关文章

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

Python模拟串口通信的示例详解

《Python模拟串口通信的示例详解》pySerial是Python中用于操作串口的第三方模块,它支持Windows、Linux、OSX、BSD等多个平台,下面我们就来看看Python如何使用pySe... 目录1.win 下载虚www.chinasem.cn拟串口2、确定串口号3、配置串口4、串口通信示例5

css中的 vertical-align与line-height作用详解

《css中的vertical-align与line-height作用详解》:本文主要介绍了CSS中的`vertical-align`和`line-height`属性,包括它们的作用、适用元素、属性值、常见使用场景、常见问题及解决方案,详细内容请阅读本文,希望能对你有所帮助... 目录vertical-ali

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

shell脚本自动删除30天以前的文件(最新推荐)

《shell脚本自动删除30天以前的文件(最新推荐)》该文章介绍了如何使用Shell脚本自动删除指定目录下30天以前的文件,并通过crontab设置定时任务,此外,还提供了如何使用Shell脚本删除E... 目录shell脚本自动删除30天以前的文件linux按照日期定时删除elasticsearch索引s

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

mysql-8.0.30压缩包版安装和配置MySQL环境过程

《mysql-8.0.30压缩包版安装和配置MySQL环境过程》该文章介绍了如何在Windows系统中下载、安装和配置MySQL数据库,包括下载地址、解压文件、创建和配置my.ini文件、设置环境变量... 目录压缩包安装配置下载配置环境变量下载和初始化总结压缩包安装配置下载下载地址:https://d

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<