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

思路:
建立一个优先级队列,存储在黄线之内的所有客户。
对于m*n之前的人,依此往窗口里排队(即优先级队列里)即可。
对于之后的人,从优先级队列里取出结束时间最少的客户,有相同的话则是窗口最小的,进入他所在的队列。
同时还要建立linetime数组,存储每个窗口排在末尾客户的结束时间。因为当窗口j新加进来一个客户,他的起始时间则是linetime[j],结束时间则是linetime[j]+服务时间,同时更新linetime[j]
最后对于查询的客户,如果起始时间>=17:00,则输出Sorry
否则输出对应的结束时间即可,这里方便起见,以分钟数存储的,所以最后还要转化一下。
参考
优先队列基础

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxx=1005;
struct customer {
	int stime=0;
	int endtime=0;
	int solvetime;
	int line;
	bool operator <(const customer&tmp)const {         //重载小于号(意思是满足下列条件的优先级小) 
		if(endtime==tmp.endtime)                       //结束时间一样选择窗口号小的入队,否则选择结束时间早的入队 
			return line>tmp.line;
		else return endtime>tmp.endtime;
	}
} cus[maxx];
int linetime[maxx];                                    
int query[maxx];
int maxtime=540;
int main(){
	int n,m,k,q;
	scanf("%d %d %d %d",&n,&m,&k,&q);
	for(int i=1;i<=k;i++) scanf("%d",&cus[i].solvetime);//记录顾客i的处理业务的所需时间
	for(int i=1;i<=q;i++) scanf("%d",&query[i]);
	/*
	  前 m*n 个顾客是不需要等待的,按照顺序可以直接进入排队而不需要等待。 
	  所以先把前m*n个顾客push进优先队列中 。 
	*/
	customer c,tmp; 
	int cnt=0; 
	memset(linetime,0,sizeof(linetime));                //一开始的每个窗口的开始时间都为0 
	priority_queue<customer>qline;
	for(int i=1;i<=m&&cnt<k;i++){
		for(int j=1;j<=n&&cnt<k;j++){
			cnt++;
			tmp.line=j;
			tmp.stime=linetime[j];
			tmp.endtime=tmp.stime+cus[cnt].solvetime;
			linetime[j]=tmp.endtime;
			cus[cnt].line=tmp.line;
			cus[cnt].stime=tmp.stime;
			cus[cnt].endtime=tmp.endtime;
			qline.push(tmp);
		}
	} 
	
	for(int i=cnt+1;i<=k;i++){
		tmp=qline.top();                          //取出结束时间最早的顾客,放入其他顾客 
		qline.pop();
		c.line=tmp.line;
		c.stime=linetime[tmp.line];
		c.endtime=c.stime+cus[i].solvetime;
		linetime[tmp.line]=c.endtime;
		cus[i].line=c.line; 
		cus[i].stime=c.stime;
		cus[i].endtime=c.endtime;	
		qline.push(c);
	}
    for(int i=1;i<=q;i++){
    	if(cus[query[i]].stime>=maxtime){
    		printf("Sorry\n");
		}
		else{
			int hour=cus[query[i]].endtime/60;
            int mins=cus[query[i]].endtime%60;
            printf("%02d:%02d\n",hour+8,mins);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值