Skip to main content

1017QueueingatBank模拟

·546 words·3 mins
WFUing
Author
WFUing
A graduate who loves coding.
Table of Contents

1017 Queueing at Bank
#

0、题目
#

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

1、大致题意
#

类似于 1014 Waiting in Line ,给出银行窗口数和办理业务人头数,并给出这些人到达的时间,计算最后人们的平均等待时间。

1.1 测试点5
#

有了 1014 Waiting in Line 作为铺垫,基本上坑点都一模一样,就是 17:00 及以前来的那批到 17:00 时还没被服务的人,哪怕过了 17:00 也会被继续服务,哪怕银行加班到凌晨也要处理这批人的业务(诶,这真的反常识啊)。

这个坑点被设计为 测试点5

./figures/f4617db893434781980506efbe5a0415.png

2、基本思路
#

设计结构体 customercounter ,然后 sort 大法排序顾客到达店的顺序。用一个 priority_queue 模拟柜台。

struct customer {
	int hh,mm,ss,time; //time为转化为秒以后的时间
	int cost; //办理业务所需时间
	int start; //开始办理时间
	bool operator >(const customer &a)const {
		if(time!=a.time) {
			return time < a.time;
		} else {
			return cost < a.cost;
		}
	}
};

struct counter {
	int id;
	int end;
	bool operator >(const counter &a)const {
		if(end!=a.end) {
			return end > a.end;
		} else {
			return id > a.id;
		}
	}
};

3、AC代码
#

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct customer {
	int hh,mm,ss,time; //time为转化为秒以后的时间
	int cost; //办理业务所需时间
	int start; //开始办理时间
	bool operator >(const customer &a)const {
		if(time!=a.time) {
			return time < a.time;
		} else {
			return cost < a.cost;
		}
	}
} cus[10005];

struct counter {
	int id;
	int end;
	bool operator >(const counter &a)const {
		if(end!=a.end) {
			return end > a.end;
		} else {
			return id > a.id;
		}
	}
} cou[105],tmp;
int n,k,ans;
priority_queue<counter,vector<counter>,greater<counter> >q1;

int toTime(int hh,int mm,int ss) {
	return hh*60*60+mm*60+ss;
}

int main() {
	cin>>n>>k;
	for(int i=1; i<=n; i++) {
		scanf("%d:%d:%d %d",&cus[i].hh,&cus[i].mm,&cus[i].ss,&cus[i].cost);
		cus[i].cost*=60;
		cus[i].time=toTime(cus[i].hh,cus[i].mm,cus[i].ss);
	}
	sort(cus+1,cus+n+1,greater<customer>());
//	cout<<cus[1].hh<<" "<<cus[1].mm<<" "<<cus[1].ss;
	ans=1;
	for(int i=1; i<=k; i++) {
		if(ans>n) {
			break;
		} else {
			tmp.id=i;
			if(cus[ans].time<=8*60*60) {
				cus[ans].start=8*60*60;
			} else {
				cus[ans].start=cus[ans].time;
			}
			tmp.end=cus[ans].start+cus[ans].cost;
			q1.push(tmp);
			ans++;
		}
	}
	while(!q1.empty()&&ans<=n) {
		tmp=q1.top();
		q1.pop();
		if(cus[ans].time>=tmp.end){
			cus[ans].start=cus[ans].time;
		}else{
			cus[ans].start=tmp.end;
		}
		tmp.end=cus[ans].start+cus[ans].cost;
		q1.push(tmp);
		ans++;
	}
	int summ=0,num=0;
	for(int i=1; i<=n; i++) {
		if(cus[i].time>17*60*60) { //坑点!!17:00到的不接待
			continue;
		} else {
			num++;
			summ+=(cus[i].start-cus[i].time);
		}
	}
	printf("%.1f\n",summ/num/60.0);
	return 0;
}

./figures/66086b1eed254ec3918ad48e7a69d26e.png