PAT A1017 Queueing at Bank (25point(s))

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 (≤10​4​​) - 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
  • 思路:模拟os中进程调度的平均等待时间
    step 1:将17点之前到的顾客push进队列customers,按时间先后顺序排好
    step 2:用一个优先队列收入所有窗口,窗口按当前结束时间的先后排列(结束的越早,优先级越大)
    step 3:遍历customers,每次取窗口队列的队首:
      case 1:若当前顾客到达时间比当前最先空闲的窗口结束时间大,说明不需要等待,来了就上:更新窗口结束时间 = 该顾客到达时间+要求服务时间
      case 2:若比当前窗口(最优的)结束时间小(说明来的时候全满的),需等待,1. 更新窗口结束时间 += 该顾客要求服务时间 2. 累加等待时间 += 窗口结束时间 - 顾客到达时间;

  • 坑点:

  1. Wrong 1:样例1 2 ,没有考虑顾客来的时候有窗口一直空闲

  2. Wrong 2:按理说,如果一个人17点前来的但是窗口全满了,等到有窗口空闲已经17点以后,这个是无效的,但题目好像是算作有效的了

  • code:普通版:算法笔记
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110, INF = 0x3fffffff;
int end_time[maxn]; 
struct cus{
	int arrive_time, process_time;
	cus(int _arr, int _pro) : arrive_time(_arr), process_time(_pro){}
};
vector<cus> customers;
int GetTime(int h, int m, int s){
	return h * 3600 + m * 60 + s;
}
bool cmp(cus a, cus b){
	return a.arrive_time < b.arrive_time;
}
int main(){
	int num_cus, num_win;
	int open_time = GetTime(8, 0, 0), close_time = GetTime(17, 0, 1);
	scanf("%d %d", &num_cus, &num_win);
	for(int i = 0; i < num_cus; ++i){
		int th, tm, ts, tp;
		scanf("%d:%d:%d %d", &th, &tm, &ts, &tp);
		int t_time = GetTime(th, tm, ts);
		if(t_time < close_time){
			customers.push_back(cus(t_time, tp <= 60 ? tp * 60 : 3600));
		}
	}
	fill(end_time, end_time + maxn, open_time);
	sort(customers.begin(), customers.end(), cmp);
	int wait_time = 0;
	for(int i = 0; i < customers.size(); ++i){
		int now = -1, Min = INF;
		for(int j = 0; j < num_win; ++j){
			if(end_time[j] < Min){
				Min = end_time[j];
				now = j;
			}
		}	
		if(end_time[now] <= customers[i].arrive_time){
			end_time[now] = customers[i].arrive_time + customers[i].process_time; 
		}else{
			wait_time += (end_time[now] - customers[i].arrive_time);
			end_time[now] += customers[i].process_time;
		}	
	} 
	if(customers.size() == 0) printf("0.0");	//测试数据中无 
	else printf("%.1f", wait_time / 60.0 / customers.size());
	return 0;
} 

  • code:优先队列
#include <bits/stdc++.h>
using namespace std;
struct cus{
	int arrive_time, process_time;
	cus(int _arr, int _pro) : arrive_time(_arr), process_time(_pro){}
};
vector<cus> customers;
struct win{
	int id, end_time;
	win(int _id, int _et) : id(_id), end_time(_et){}
	bool operator < (const win & a) const {
		return end_time > a.end_time;
	}
};
priority_queue<win> pq;
int GetTime(int h, int m, int s){
	return h * 3600 + m * 60 + s;
}
bool cmp(cus a, cus b){
	return a.arrive_time < b.arrive_time;
}
int main(){
	int num_cus, num_win, open_time = GetTime(8, 0, 0), end_time = GetTime(17, 0, 1);
	scanf("%d %d", &num_cus, &num_win);
	for(int i = 0; i < num_cus; ++i){
		int th, tm, ts, tp;
		scanf("%d:%d:%d %d", &th, &tm, &ts, &tp);
		int t_time = GetTime(th, tm, ts);
		if(t_time < end_time){
			customers.push_back(cus(t_time, tp * 60));
		}
	}
	sort(customers.begin(), customers.end(), cmp);
	for(int i = 0; i < num_win; ++i){
		pq.push(win(i, open_time));	//初始化窗口队列 
	}	
	int valid_cus = customers.size(), wait_time = 0;
	for(int i = 0; i < valid_cus; ++i){
		win now = pq.top();
//		if(now.end_time < end_time){	//Wrong 2: 样例5 
			pq.pop();
//			cnt_served++;
			if(now.end_time <= customers[i].arrive_time){
				now.end_time = (customers[i].arrive_time + customers[i].process_time);
			}else{
				wait_time += (now.end_time - customers[i].arrive_time);	//Wrong 1:样例 1 2如果结束的比顾客来的早,他不需要等待!! 
				now.end_time += customers[i].process_time;
			}
			pq.push(now); 
//		}
	} 
	if(valid_cus == 0) printf("0.0");	//测试数据中无 
	else printf("%.1f", wait_time / 60.0 / valid_cus);
	return 0;
} 

发布了271 篇原创文章 · 获赞 5 · 访问量 6508

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/104281278