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
心得
这个题是常规题,计算银行结束时间就可以了。不过也用了50分钟,做的还是太慢了。学到了precision()这个函数是四舍五入输出的。和float转int不同。int取数直接把后面就舍弃掉了。
#include <iostream>
#include <string>
#include <algorithm>
#include<iomanip>
using namespace std;
int N, K;//N顾客总数<10^4 //K窗口数量
typedef struct {
int getTime;
int lastTime;
}People;
People people[10000];
int windows[100];
int stringToTime(string time) {
int iT=0;
iT += stoi(time.substr(0, 2))*3600;
iT += stoi(time.substr(3, 2)) * 60;
iT += stoi(time.substr(6, 2));
return iT;
}
bool sortPeo(People people1, People people2) {
return people1.getTime < people2.getTime;
}
int findMin() {
int min=99999999;
int index=0;
for (int i = 0; i < K; i++)
{
if (windows[i] < min) {
min = windows[i];
index = i;
}
}
return index;
}
int main() {
cin >> N >> K;
int peopleNumber = 0;
for (int i = 0; i < N; i++)
{
string times;
int P;
cin >> times >> P;
if (stringToTime(times) > 17 * 3600) continue;
people[peopleNumber].getTime = stringToTime(times);
people[peopleNumber].lastTime = P*60;
peopleNumber++;
}
for (int i = 0; i < K; i++)
{
windows[i] = 8 * 3600;
}
sort(people, people + peopleNumber, sortPeo);
//这里还需要给顾客排队
int sumWaitTime = 0;
for (int i = 0; i < peopleNumber; i++)
{
int min = findMin();
//用户到的时间比窗口结束的早
// 直接窗口时间-用户时间=等待时间
// 窗口时间是现在窗口时间加用户持续时间
//比结束的晚
// 窗口时间 = 用户开始+用户结束
// 无等待时间
if (people[i].getTime < windows[min]) {
sumWaitTime += windows[min] - people[i].getTime;
windows[min] += people[i].lastTime;
}
else {
windows[min] = people[i].getTime+ people[i].lastTime;
}
}
float res = (((float)sumWaitTime / (float)peopleNumber)) / 60;
cout <<fixed<<setprecision(1)<< res;
return 0;
}