题目如下:
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 10Sample Output:
8.2
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <iomanip>
#define START 28800
#define END 61200
using namespace std;
struct Customer {
int arriveTime;
int processTime;
int waitingTime;
int startTime;
int endTime;
};
int N, K;
vector<Customer> customers;
queue<Customer> windows[101];
double total = 0;
int countNum = 0;
bool compara(Customer a, Customer b)
{
if (a.arriveTime < b.arriveTime)
return true;
else
return false;
}
int findEmptyWindow()
{
for (int i = 0;i < K;i++)
{
if (windows[i].size() == 0)
return i;
}
return -1;
}
int findEarliestWindow()
{
int min = -1;
for (int i = 0;i < K;i++)
{
if (windows[i].size() != 0)
{
min = i;
break;
}
}
if (min == -1)
return min;
for (int i = 0;i < K;i++)
{
if (windows[i].size() != 0 && windows[min].front().endTime > windows[i].front().endTime)
{
min = i;
}
}
return min;
}
void enterWindow(Customer c)
{
if (c.arriveTime <= END)
{
int index = findEarliestWindow();
if (index != -1) //不是全空
{
if (c.arriveTime >= windows[index].front().endTime)
{
total += (double)windows[index].front().waitingTime / 60;
countNum += 1;
c.startTime = c.arriveTime;
c.waitingTime = 0;
c.endTime = c.startTime + c.processTime;
windows[index].pop();
windows[index].push(c);
}
else
{
int i = findEmptyWindow();
if (i != -1) //有空窗口
{
if (c.arriveTime < START)
{
c.startTime = START;
}
else
{
c.startTime = c.arriveTime;
}
c.waitingTime = c.startTime - c.arriveTime;
c.endTime = c.startTime + c.processTime;
windows[i].push(c);
return;
}
else
{
total += (double)windows[index].front().waitingTime / 60;
countNum += 1;
c.startTime = windows[index].front().endTime;
c.waitingTime = c.startTime - c.arriveTime;
c.endTime = c.startTime + c.processTime;
windows[index].pop();
windows[index].push(c);
}
}
}
else //全空
{
if (c.arriveTime < START)
c.startTime = START;
else
c.startTime = c.arriveTime;
c.waitingTime = c.startTime - c.arriveTime;
c.endTime = c.startTime + c.processTime;
windows[0].push(c);
}
}
}
void outWindow()
{
for (int i = 0;i < K;i++)
{
if (windows[i].size() != 0)
{
total += (double)windows[i].front().waitingTime / 60;
countNum += 1;
windows[i].pop();
}
}
}
int main()
{
cin >> N >> K;
string s;
Customer temp;
for (int i = 0;i < N;i++)
{
cin >> s >> temp.processTime;
temp.arriveTime = atoi(s.substr(0, 2).c_str()) * 60 * 60 + atoi(s.substr(3, 5).c_str()) * 60 + atoi(s.substr(6, 8).c_str());
temp.processTime *= 60;
customers.push_back(temp);
}
sort(customers.begin(),customers.end(),compara);
for (int i = 0;i < N;i++)
{
enterWindow(customers[i]);
}
outWindow();
cout << fixed << setprecision(1) << total / countNum << endl;
}
一次通过全部用例。