流程模拟+多条件多细节+队列模拟+递增时间判断 1014 Waiting in Line (30分)

本文介绍了一个银行排队系统的模拟实现,通过使用队列数据结构来跟踪每个窗口的顾客等待情况,模拟顾客选择最短队列进行等待的过程,最终计算并输出每位顾客完成业务的确切时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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, 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 (≤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

解题

给定排队人数,窗口数,等待区人数,排队规则,求每个人成功办理业务的时间;

用队列模拟排队,队首即为正在处理的人;

1.构造Cus对象,内含所需处理时间和结束时间,以及对象编号;

#include<iostream>
#include<queue> 

using namespace std;
const int MAXK =1001;
const int MAXN =21;
const int MAXM = 11;
   
struct Cus{
	int num;
	int protime;
	int endtime=0;
};
Cus T[MAXK];      //顾客 

T数组存放所有的顾客;

2.构造队列数组,存放正在排队的顾客

int N,M,K,q;  //N<=20(窗口数); M<=10(黄线距离) ; K<=1000(人数); Q<=1000(要查询的人)
queue<Cus> Q[MAXN];
int t=1; //等待入队的人的编号 
void input()
{
	cin>>N>>M>>K>>q;
	for(int i=1;i<=K;i++){
		cin>>T[i].protime;
		T[i].num=i;
	}
	//后续Q个查询人数 
	for(int i=0;i<M;i++)
		for(int j=0;j<N;j++)
			Q[j].push(T[t++]);
	
	for(int i=0;i<N;i++)
		Q[i].front().endtime=Q[i].front().protime;
	
	//Q里存放了队列的人数;
}

将一开始进入等待区的顾客放入队列数组,且初始化队首的完成时间;

3.模拟整个流程,计算每个人的完成时间,放在数组Time中;

每当一个顾客办理完成出队后,更新下一个办理者的完成时间——办理时间+当前时间,并且在该队列中加入等候区外的下一个人;
最后需要将队列中的人全部出队,并且得到完成时间——没有更新完成时间的(在队列中但没有开始办理)完成时间为0,输出sorry即可;

int Time[MAXK];    //算出每个人结束的时间 
void calculate()
{
	for(int i=0;i<540;i++)           //17点倒计时 
	{
		//过去i分钟;
		for(int j=0;j<N;j++)        //遍历整个数组 
		if(!Q[j].empty()&&i==Q[j].front().endtime) //有个队头完成了 
		{
			Time[Q[j].front().num]=i;
			Q[j].pop();
			Q[j].front().endtime=Q[j].front().protime+i;
			if(t<=K)	Q[j].push(T[t++]);
		}
	}
	for(int i=0;i<N;i++)
	if(!Q[i].empty()){
		Time[Q[i].front().num]=Q[i].front().endtime;
			Q[i].pop();
	}      
}

注意点:在540分钟内,即17:00前入队的对象,需要输出结束时间;
540分钟即以后开始办理的对象输出sorry;

4.输出函数

void output()
{
	int Hour;
	int Minute;
	for(int i=0;i<q;i++)
	{	
		int tmp;
		cin>>tmp;
		Hour=8;
		Minute=0;
		if(Time[tmp]==0) cout<<"Sorry"<<endl;
		else 
		{
			Hour+=Time[tmp]/60;
			Minute=Time[tmp]%60;
			printf("%02d:%02d\n",Hour,Minute);
		}
	}
}

5.main函数

int main()
{
	input();
	calculate();
	output();
}

总结
该题关键在于读懂题意,用合适容器模拟整个流程;
产生移动的为顾客,构造顾客的结构对象,保存各种参数;
排队需要入队操作,调用队首办理时间,故队列保存即可;
遍历可用时间,0~540分钟,也可每次得到队首的最短时间,一次跳多个i,不过一共就540次遍历不会太大,(每过一分钟要遍历整个队列的队首,N最多10);

给出的N和M都不大,无需考虑节省时间,每分钟遍历一次即可;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值