1014 Waiting in Line

题目来源:PAT (Advanced Level) Practice

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.
  • Customeri​ will take Ti​ 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 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 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

words:

transactions 事物,处理,交易        respectively 各自的,分别地  

题意:   

有n个窗口(每个窗口可以排队m个人)和k位用户,每位用户选择队伍人数少的窗口排队(若不唯一则选择窗口编号小的 ),求给定用户的服务结束时间;

思路:

1. 求出所有用户的服务结束时间,用数组serveEnd[]记录;所有的时间都转化为分钟来计时

2. 使用数组costomer[]来保存每位用户的服务时长;

3. 使用数组window[]来记录窗口每次的服务结束时间,初始化为窗口前第一个人的服务结束时间,当窗口前无人时则将其置为下班时间(17:00)或无穷大;

4. 使用队列数组windowPeople[]来存储每个窗口队伍的用户编号;

5. 不断从窗口中找出最先结束当前服务的窗口编号index,然后将对应用户从该窗口队伍中移除,同时记录用户的服务结束时间,然后将在黄线外(尚未排入队列中)的用户排入该队末尾并重置窗口的服务结束时间;

6. 按查找要求输出对应用户的服务结束时间即可,对于下班前进行服务的用户输出其服务结束时间(不管在下班前服务是否结束),对于下班前没有接受到服务的用户输出”Sorry“;

//PAT ad 1014 Waiting in Line
#include <iostream> 
using namespace std;
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#define N 10005
#define K 105

int startTime=8*60; 	//上班时间 
int endTime=17*60;		//下班时间 

int findWindow(int *window,int n)	//返回服务结束时间最早的窗口编号 
{
	int index=0;
	for(int i=1;i<n;i++)
	{
		if(window[i]<window[index])
			index=i;
	}
	return index;
}
int main()
{
	int n,m,k,q,i;			//窗口数,每队最大人数,总人数,查询次数 
	cin>>n>>m>>k>>q;
	int costomer[k];		//用户的服务时长,分钟  
	int window[n];			//窗口的服务结束时间 
	vector<queue<int> > windowPeople(n);	//窗口的排队用户的编号 
	
	for(i=0;i<k;i++)		//输入
		cin>>costomer[i];
	
	fill(window,window+n,endTime); 			//初始化窗口服务时间为服务结束时间 (避免没人排队时被选到) 
	
	for(i=0;i<k&&i<n*m;i++)					//初始化窗口排队的人的编号 
	{
		windowPeople[i%n].push(i);			//将用户i排入队i%n中去 
		if(i<n)
		window[i]=startTime+costomer[i];	//窗口的第一次服务结束时间 
	}
	int now=i;		//尚未排队的第一个人的编号 
	
	int serveEnd[k]={0};				//用户的服务结束时间 
		
	for(int j=0;j<k;j++)		//计算每个人的服务结束时间 
	{	
		int index=findWindow(window,n);     //最先结束服务的窗口号
		i=windowPeople[index].front(); 
		windowPeople[index].pop();  //用户i服务完毕,出队
		serveEnd[i]=window[index];  //用户i的服务结束时间
		
		if(!windowPeople[index].empty())		//该窗口还有人排队 
		{
			int nextI=windowPeople[index].front();		//该窗口的下一为用户			
			window[index]+=costomer[nextI];		//重置窗口的服务结束时间 			
		}
		else	
			window[index]+=endTime;		//窗口没人排队时,将服务结束时间置为下班时间 
		
		if(now<k)						//有人在黄线外 
			windowPeople[index].push(now++);		//排入窗口index的队伍中 		 
	}
	
	int x;	
	while(q--)		//查询用户的服务结束时间 
	{
		cin>>x;
		if(serveEnd[x-1]-costomer[x-1]<endTime)
		{
			cout<<setw(2)<<setfill('0')<<serveEnd[x-1]/60<<":";		//时 
			cout<<setw(2)<<setfill('0')<<serveEnd[x-1]%60<<endl;	//分 
		}	
		else
			cout<<"Sorry"<<endl;	
	} 
		
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值