1017. Queueing at Bank (25)
时间限制 400 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue
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
这道题的代码还是写的比较满意的,用了两个优先队列模拟排队的过程。优先队列默认从小到大排列,top()出的是最大元,对于里面存的是结构体custom,重载了运算符<,对于里面元素是int的window优先队列,priority_queue<int, vector<int>, greater<int>>window;三个参数分别表示队列中的元素类型,指定容器,排序规则为从大到小,中greater要#include<functional>;window中存的是当前窗口结束的时间。
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cstdio>
#include<functional>
using namespace std;
struct custom
{
public:
int arrival;
int process;
string time;
custom(string a, int b) :time(a), process(b) {
arrival = Transtime(time);
};
bool operator < (const custom& c1)const
{
return this->arrival > c1.arrival;
}
private:
int Transtime(string time)
{
int res = 0;
res = ((time[0] - '0') * 10 + time[1] - '0') * 3600 +
((time[3] - '0') * 10 + time[4] - '0') * 60 +
(time[6] - '0') * 10 + time[7] - '0';
return res;
}
};
int main()
{
int N, K;
cin >> N >> K;
priority_queue<custom>people;
priority_queue<int, vector<int>, greater<int>>window;
string time;
int i, pro, tmp;
for (i = 0; i < N; i++)
{
cin >> time >> pro;
if (time <= "17:00:00")
{
custom person(time, pro);
people.push(person);
}
}
vector<int>waiting;
int now = 8 * 60 * 60;
int j = people.size();
for (i = 0; i < j; i++)
{
custom firstperson = people.top();
people.pop();
if (i < K)
{
if (firstperson.arrival < now)
{
waiting.push_back(now - firstperson.arrival);
window.push(now + firstperson.process*60);
}
else
{
waiting.push_back(0);
window.push(firstperson.arrival + firstperson.process * 60);
}
}
else
{
now = window.top();
window.pop();
if (firstperson.arrival < now)
{
waiting.push_back(now - firstperson.arrival);
window.push(now + firstperson.process * 60);
}
else
{
waiting.push_back(0);
window.push(firstperson.arrival + firstperson.process * 60);
}
}
}
double average = 0;
vector<int>::iterator it;
for (it = waiting.begin(); it != waiting.end(); it++)
average += *it;
average = average / (waiting.size() * 60);
printf("%.1f", average);
return 0;
}