PAT 1017 Queueing at Bank

1017 Queueing at Bank

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤ 1 0 4 10^4 104) - the total number of customers, and K (≤100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:
For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:

7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:

8.2

解题思路:
利用优先队列模拟。

#include <iostream>
#include<cstdio>
#include<queue> 
#include<algorithm>
#include<cmath>
#define ON 8*60*60//开始时间
#define OFF 17*60*60//不能超过的时间
using namespace std;
int num(char c){
	return c-'0';
}
struct node{
	int start,last,window,time,wait;//开始时间,最后执行完该顾客需求时的时间,顾客所在窗口,顾客需求时间,等待时间
	int h,m,s;
}cus[10010];
int N,K;
bool cmp1(node &a,node &b){//按出现时间排序
	if(a.h!=b.h) return a.h<b.h;
	if(a.m!=b.m) return a.m<b.m;
	if(a.s!=b.s) return a.s<b.s;
	return a.time<b.time;
}

struct cmp{//利用优先队列求出当前最快结束的窗口
	bool operator()(node a,node b){
		if(a.last!=b.last)
			return a.last>b.last;//降序 
		return a.window>b.window;
	}
}; 

priority_queue<node,vector<node>,cmp>Q;
int cnt[110];
int main(int argc, char** argv) {
	scanf("%d%d",&N,&K);
	for(int i=0;i<N;i++){
		char input[10];
		scanf("%s%d",input,&cus[i].time);
		cus[i].time*=60;//记得全部化成秒
		cus[i].h=num(input[0])*10+num(input[1]);
		cus[i].m=num(input[3])*10+num(input[4]);
		cus[i].s=num(input[6])*10+num(input[7]);
		cus[i].start=cus[i].h*60*60+cus[i].m*60+cus[i].s;//s 
		if(cus[i].h>17||(cus[i].h==17&&(cus[i].m>0||cus[i].s>0))){//超过17点,舍弃
			i--;
			N--;
		}
	}
	sort(cus,cus+N,cmp1);

	for(int i=0;i<K;i++){//利用cnt[i]表示第i个窗口最后结束所需要的时间
		cnt[i]=ON;
	}
	int sum=0,ind=-1;
	for(int i=0;i<N;i++){
		if(Q.size()<K){
			ind=cus[i].window=i%K;
			cus[i].last=cus[i].time+max(cnt[ind],cus[i].start);//这个地方有可能出现的时间晚于前i个窗口执行的时间,中间有空余;第二个情况是出现的时间早于8点,均能处理
			cus[i].wait=cus[i].last-cus[i].time-cus[i].start;//第i个顾客的等待时间
			Q.push(cus[i]);
			cnt[ind]=cus[i].last;//更新每个窗口最后的时间
		}
		else{
			node tmp=Q.top();
			Q.pop();
			ind=cus[i].window=tmp.window;	
			cus[i].last=cus[i].time+max(cnt[ind],cus[i].start);
			cus[i].wait=cus[i].last-cus[i].time-cus[i].start;
			Q.push(cus[i]);
			cnt[ind]=cus[i].last; 
		}
	}
	
	int count=0;
	for(int i=0;i<N;i++){
//		if(cus[i].last<=OFF){//超过17点?? 这个地方表示最后的执行时间超过17点也符合题意,只有到达时间超过17点不算等待,因此舍去这种情况。
			count++;
			sum+=cus[i].wait;
//		}
	}
	printf("%.1lf",sum/(60.0*count));
	return 0;
}
posted @ 2018-10-14 20:02  xzhws  阅读(27)  评论(0编辑  收藏  举报