A1017 Queueing at Bank (25 point(s))

queue

1. 原文

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:

1
2
3
4
5
6
7
8
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:

1
8.2

2. 解析

n个人等待被服务,k个窗口提供服务,服务时间为8:00-17:00,求平均等待时间

记录用户的秒时间 h*3600+m*60+s ,服务时间以秒为单位 time*60

windows数组表示某个窗口的结束时间,每一个客户到来的时候,选择最早结束的窗口,如果最早结束时间比他还早,那么他一来就可以被服务,更新windows的值;如果最早结束时间比他晚,他需要等待,累加等待时间,更新等待总时间。

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
int cometime,usetime;
};
vector<node> customers;
bool cmp(node a,node b){
return a.cometime<b.cometime;
}
int windows[110];
int main(){
int n,k;
scanf("%d%d",&n,&k);
int h,m,s;
for (int i = 0; i < n; ++i)
{
node temp;
scanf("%d:%d:%d %d",&h,&m,&s,&temp.usetime);
temp.cometime = h*3600+m*60+s;
temp.usetime*=60;
if (temp.cometime>17*3600)
{
continue;
}
customers.push_back(temp);
}
sort(customers.begin(),customers.end(),cmp);
fill(windows,windows+110,8*3600);
double result = 0;
for (int i = 0; i < customers.size(); ++i)
{
int index = -1;
int min = 1<<30;
for (int j = 0; j < k; ++j)
{
if (windows[j]<min)
{
min = windows[j];
index = j;
}
}
if (windows[index]<=customers[i].cometime)
{
windows[index]=customers[i].cometime+customers[i].usetime;
}else{
result +=(windows[index]-customers[i].cometime);
windows[index]+=customers[i].usetime;
}
}
if (customers.size()==0)
{
printf("0.0\n");
}else{
printf("%.1f\n",result/60.0/customers.size());
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1017/
  • 发布时间: 2019-09-03 12:35
  • 更新时间: 2021-10-29 13:59
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!