pat

题目

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

思路

思路参照陈越姥姥的《数据结构》8.1银行排队问题,细节有点多,很容易写错。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include <vector>
#include <queue>
using namespace std;
int N,K;
int crtTime;
int totalTime = 0;
int cusNum;
struct {
int arrive,process;
friend bool operator< (Customer c1,Customer c2){
return c1.arrive>c2.arrive;
}
};
priority_queue<Customer> waitingLine;
vector<int> windowsTime;

int time(int hh,int mm,int ss){
return hh*3600+mm*60+ss;
}
int findNextWindows(){
int nextWin;
int minTime = 999999;
for(int i=0;i<K;i++){
if(windowsTime[i]<minTime){
minTime = windowsTime[i];
nextWin = i;
}
}
for(int i=0;i<K;i++) windowsTime[i]-=minTime;
crtTime+=minTime;
return nextWin;
}

int main()
{
cin >> N >> K;
windowsTime.resize(K);
fill(windowsTime.begin(),windowsTime.end(),0);
int hh,mm,ss,process;
char c;
for(int i=0;i<N;i++){
cin >> hh >> c;
cin >> mm >> c;
cin >> ss >> process;
if(time(hh,mm,ss)<=time(17,0,0))
waitingLine.push(Customer{time(hh,mm,ss),process*60});
}
cusNum = waitingLine.size();

crtTime = time(8,0,0);
while(!waitingLine.empty()){
Customer cus = waitingLine.top();
waitingLine.pop();
int nextWin = findNextWindows();

if(cus.arrive<=crtTime){
totalTime+= crtTime-cus.arrive;

}
else{
for(int i=0;i<K;i++){
windowsTime[i] = max(windowsTime[i]-(cus.arrive-crtTime),0);
}
crtTime = cus.arrive;
}
windowsTime[nextWin] = cus.process;
}
cout.setf(ios::fixed);
cout.precision(1);
cout << double(totalTime)/cusNum/60<<endl;
return 0;
}