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
题目大意:模拟排队,有K个窗口,给出每个顾客到达的时间,以及需要服务的时间;求每个顾客的平均等待时间;每个窗口被占用的时间不超过1小时;八点才开始服务,五点钟之后到的顾客不被服务,也不计入总数;
分析:输入到达时间和服务时间,如果到达时间在17点以后,则不记录;按照到达时间的先后顺序排序;对于每一个顾客,选取窗口最早空闲的,如果有多个,就选择其中编号最小的(题目没说要选最小的,其实可以随便选一个,咱们这里就选编号最小的);如果到达的时间大于最早空闲的时间,服务开始的时间即为到达的时间,否则为窗口最早空闲的时间。
题目中还提到,每个用户不能占用窗口超过一个小时,所以输入的时候,如果服务时间超过一个小时,就改成一个小时;
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int CLOSE = 3600 * 17;
const int OPEN = 3600 * 8;
struct node{
int arvT, costT;
};
bool cmp(node A, node B) {return A.arvT < B.arvT;}
vector<node> V;
int freeT[101];//窗口最早空闲时间
int N, K, WaitT = 0;
int selectW(){
int min = 123123123, ret;
for (int i = 0; i < K; i++){
if (freeT[i] < min){
min = freeT[i];
ret = i;
}
}
return ret;
}
int main(){
scanf("%d%d", &N, &K);
for (int i = 0; i < N; i++){
int h, m, s, p;
scanf("%d:%d:%d %d", &h, &m, &s, &p);
node tmp;
tmp.arvT = 3600 * h + 60 * m + s;
tmp.costT = p>60? 60*60 : 60*p;
if(tmp.arvT<= CLOSE)
V.push_back(tmp);
}
sort(V.begin(), V.end(), cmp);
fill(freeT, freeT + 101, OPEN);
for (int i = 0; i < V.size(); i++) {
int w = selectW();
if (freeT[w] > V[i].arvT){
WaitT += freeT[w] - V[i].arvT;
freeT[w] += V[i].costT;
}
else
freeT[w] = V[i].arvT + V[i].costT;
}
printf("%.1f", WaitT*1.0 / V.size() / 60);
return 0;
}