1017 Queueing at Bank (25 分)

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

Solution Way:

  • 以秒爲單位處理數據。
  • 定義消費者結構體,包含到達時間和處理業務所需時間。
  • 8點之前到達的消費者需要等待,17點之後到達的消費者則銀行不爲其服務。
  • 思路:
    • 以消費者到達時間排序
    • 讓一部分消費者去每一個窗口辦理業務(即每個窗口有一個消費者)
    • 找出最先辦完業務的消費者(稱之爲‘舊消費者’),並記錄該消費者所在的窗口
    • 計算舊消費者辦理完業務的時間,分兩種情況:
      • 舊消費者到達時,該窗口空閒,則他直接去辦理業務
      • 舊消費者到達時,該窗口已在辦理業務,則他需要等待
    • 更改辦理業務的消費者(移舊換新),計算新的消費者等待時間

Solution Code:

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

typedef struct customer
{
	int arrive;
	int process;
}customer;

int windows_time[101]; // 計時器
int windows_queue[101] = {0}; // 當前正在處理業務的消費者
customer cus[10002] = {0};
double total_wait = 0;

bool compare(customer a, customer b)
{
	return a.arrive < b.arrive;
}

int main()
{
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		int hour, minute, second, process;
		scanf("%d:%d:%d %d", &hour, &minute, &second, &process);
		cus[i].arrive = 3600*hour + 60*minute + second;
		cus[i].process = 60*process;
	}
	// sort by arrive  small --> big
	sort(cus, cus+n, compare);

	//08:00:00 = 28800sec  17:00:00 = 61200sec
	for (int i=0; i<101; ++i)
        windows_time[i] = 28800;
	int total_customers = n;
	// 先爲每一個窗口安排一個消費者
	for (int i = 0; i < k; ++i) {
        if (i >= n) break; //消費者的數目小於窗口數目
        if (cus[i].arrive > 61200) {
			total_customers = i;
			break;
		}
		if (cus[i].arrive < 28800) //八點之前到達
			total_wait += (28800 - cus[i].arrive);
		windows_queue[i] = i;
	}

	for (int i = k; i < n; ++i) {
		if (cus[i].arrive > 61200) {
			total_customers = i;
			break;
		}
		int early_time = 999999999;
		int windows_id;
		// 找到最先辦完業務的窗口
		for (int j = 0; j < k; ++j) {
			if (windows_time[j]+cus[windows_queue[j]].process < early_time) {
				early_time = windows_time[j] + cus[windows_queue[j]].process;
				windows_id = j;
			}
		}

        int last_customer = windows_queue[windows_id]; // 辦理完業務的顧客
        // 辦完業務的消費者離開,新的消費者辦理業務
		windows_queue[windows_id] = i;
		// 更新辦完業務的消費者離開時的窗口時間
		if (windows_time[windows_id] >= cus[last_customer].arrive)
            windows_time[windows_id] +=  cus[last_customer].process;
        else
            windows_time[windows_id] = cus[last_customer].arrive + cus[last_customer].process;
        // 計算新的消費者等待的時間
        if (windows_time[windows_id] >= cus[i].arrive)// 來早了,需要等待
            total_wait += (windows_time[windows_id] - cus[i].arrive);
	}
	printf("%.1lf\n", total_wait/(60*total_customers));

	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章