题目
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
解题思路及注意事项:
这题并不难,把时间转换为距离0点的秒数,然后给到达时间排序,循环依次从窗口中选择时间最小的来服务,并对窗口时间进行更新
需要注意的是
- 顾客所需的处理时间如果是>60分钟的话自动变成60分钟
- 17:00:00及之前的顾客都会被服务,即使服务的时间超过24点
代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<iomanip>
#include<climits>
using namespace std;
struct Customer {
string arrive;
int arr_int;
int get_server;
int process;
Customer(int lea = -1) { get_server = lea; }
};
bool compare(Customer c1, Customer c2);
void serve(vector<Customer>&ctm_vec,int k);
void timeProcess(vector<Customer>&ctm_vec);
float average(vector<Customer>&ctm_vec);
int main() {
int i, n, k;
vector<Customer>ctm_vec;
Customer customer;
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> customer.arrive >> customer.process;
ctm_vec.push_back(customer);
}
sort(ctm_vec.begin(), ctm_vec.end(), compare);
timeProcess(ctm_vec);
serve(ctm_vec, k);
cout << setiosflags(ios::fixed) << setprecision(1);
cout << average(ctm_vec);
}
bool compare(Customer c1, Customer c2) {
if (c1.arrive < c2.arrive)
return true;
else
return false;
}
void serve(vector<Customer>&ctm_vec,int k) {
int i, j, min, m_index;
int *window = new int[k];
for (j = 0; j < k; j++) {
window[j] = 8 * 3600;
}
for (i = 0; i < ctm_vec.size(); i++) {
min = INT_MAX;
if (ctm_vec[i].arr_int>61200)break;
if (ctm_vec[i].process>60)ctm_vec[i].process = 60;
for (j = 0; j < k; j++) {
if (min > window[j]) {
min = window[j];
m_index = j;
}
}
ctm_vec[i].get_server = window[m_index] > ctm_vec[i].arr_int ? window[m_index] : ctm_vec[i].arr_int;
window[m_index] = ctm_vec[i].get_server + ctm_vec[i].process * 60;
}
}
void timeProcess(vector<Customer>&ctm_vec) {
int interval = 0, i;
int hour, min, sec;
for (i = 0; i < ctm_vec.size(); i++) {
hour = (ctm_vec[i].arrive[0] - '0') * 10 + (ctm_vec[i].arrive[1] - '0');
min= (ctm_vec[i].arrive[3] - '0') * 10 + (ctm_vec[i].arrive[4] - '0');
sec= (ctm_vec[i].arrive[6] - '0') * 10 + (ctm_vec[i].arrive[7] - '0');
interval = hour * 3600 + min * 60 + sec;
ctm_vec[i].arr_int = interval;
}
}
float average(vector<Customer>&ctm_vec) {
int i, count=0;
float total = 0;
for (i = 0; i < ctm_vec.size(); i++) {
if (ctm_vec[i].get_server != -1) {
total += (ctm_vec[i].get_server - ctm_vec[i].arr_int);
count++;
}
}
if (count == 0)return total;
else
return total / (60 * count);
}