A1017. 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 (<=10000) - 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 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<math.h>
 5 using namespace std;
 6 typedef struct{
 7     int come;
 8     int process;
 9 }info;
10 info people[10001];
11 int window[101];
12 int N, K;
13 bool cmp(info a, info b){
14     return a.come < b.come;
15 }
16 int main(){
17     int hh, mm, ss, len;
18     scanf("%d%d", &N, &K);
19     for(int i = 0; i < N; i++){
20         scanf("%d:%d:%d %d", &hh, &mm, &ss, &len);
21         people[i].come = hh * 3600 + mm * 60 + ss;
22         people[i].process = len * 60;
23     }
24     sort(people, people + N, cmp);
25     int wait = 0, early = 8 * 3600, late = 17 * 3600;
26     fill(window, window + K, early);
27     int cnt = 0;
28     for(int i = 0; i < N; i++){
29         int index = -1, minT = 100000000;
30         for(int j = 0; j < K; j++){
31             if(window[j] < minT){
32                 minT = window[j];
33                 index = j;
34             }
35         }
36         if(people[i].come > late)
37             break;
38         cnt++;
39         if(people[i].come >= window[index]){
40             window[index] = people[i].process + people[i].come;
41         }else{
42             wait += (window[index] - people[i].come);
43             window[index] += people[i].process;
44         }
45     }
46     double AVG = (double)wait / (double)(cnt * 60);
47     printf("%.1f", AVG);
48     cin >> N;
49     return 0;
50 }
View Code

总结:

1、模拟排队和服务的问题。不要把window数组仅仅设置为占用和不占用,而是用windows[ i ]记录该窗口可被使用的时间。初始化时都被置为8:00,即8点之后才可服务。

2、由于题目给出的顾客是乱的,先按时间排序。一次处理每一个顾客,对每一个顾客,选择一个可被使用的时间最早的窗口对其处理,如果顾客来的时间早于窗口可服务时间,则等待时间累加,并修改窗口可服务时间;如果晚于,则可立即服务没有等待时间,但依旧修改窗口可服务时间。如果顾客晚于17点或最早可被使用的窗口晚于17点则无法服务。

3、为了便于计算,所有时间换算成秒。没有被服务的顾客不计入等待时间。

posted @ 2018-02-26 17:13  ZHUQW  阅读(150)  评论(0编辑  收藏  举报