1017. Queueing at Bank
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
题目链接:PAT-1017
题目大意:给出n个人,k个窗口。接下来输入每个人到达时间以及办业务的时间。问平均每个人的等待时间是多少,以分钟计算
题目思路:处理业务从[8:00,17:00]。早于8:00需要等待到8:00开始处理,到达时间晚于17:00则不处理。如果17:00之前到达但是处理到17:00之后,是允许的。
另外,计算平均等待时间,晚于17:00的人是不计算进去的!!(因为这个调好久)
其他直接模拟就可以了
以下是代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
int t[105];
struct node
{
int time,need;
}p[10005];
bool cmp(node a,node b)
{
return a.time < b.time;
}
int main()
{
int n,k;
cin >> n >> k;
int h,m,s,ans = 0;
int begin = 8 * 60 * 60;
int end = 17 * 60 * 60;
for (int i = 0; i < k; i++)
{
t[i] = begin;
}
for (int i = 0; i < n; i++)
{
scanf("%d:%d:%d %d",&h,&m,&s,&p[i].need);
p[i].time = (h * 60 + m) * 60 + s;
p[i].need *= 60;
}
sort(p, p + n,cmp);
int cnt = 0;
for (int i = 0; i < n; i++)
{
if (p[i].time > end) continue;
int poi = 0;
for (int j = 0; j < k; j++)
{
if (t[j] < t[poi]) poi = j;
}
if (t[poi] >= p[i].time)
{
ans += t[poi] - p[i].time;
t[poi] += p[i].need;
}
else t[poi] = p[i].time + p[i].need;
cnt++; //记录处理业务的人数
}
printf("%.1f\n",(ans * 1.0 / (60 * cnt)));
return 0;
}