您的位置:首页 > 产品设计 > UI/UE

PAT-A-1017 . Queueing at Bank (25)

2018-01-30 10:05 423 查看

PAT-A-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

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

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



大模拟,模拟队列,代码如下:

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

struct Node
{
int h, m, s;
int time;
};

vector<Node> input;

bool cmp(Node a, Node b)
{
if (a.h != b.h)
return a.h < b.h;
else if (a.m != b.m)
return a.m < b.m;
else
return a.s < b.s;
}

struct window
{
int hh, mm, ss;
int index;
};

bool cmp1(window a, window b)
{
if (a.hh != b.hh)
return a.hh < b.hh;
else if (a.mm != b.mm)
return a.mm < b.mm;
else
return a.ss < b.ss;
}

vector<window> win;

int main()
{
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
Node nod;
scanf("%d:%d:%d %d", &nod.h, &nod.m, &nod.s, &nod.time);
input.push_back(nod);
}
sort(input.begin(), input.end(), cmp);

int total = 0, cnt = 0;
for (int i = 0; i < k; i++)
{
window w;
if (input[i].h < 8)
{
w.hh = 8 + input[i].time / 60;
w.mm = input[i].time % 60;
w.ss = 0;
}
else
{
w.hh = input[i].h + (input[i].m + input[i].time) / 60;
w.mm = (input[i].m + input[i].time) % 60;
w.ss = input[i].s;
}
win.push_back(w);

if (input[i].h < 8)
{
total += (8 - input[i].h) * 3600 - input[i].m * 60 - input[i].s;
}
if (input[i].h < 17 || (input[i].h == 17 && input[i].m == 0 && input[i].s == 0))
cnt++;
}

for (int i = k; i < n; i++)
{
if (input[i].h < 17 || (input[i].h == 17 && input[i].m == 0 && input[i].s == 0))
{
sort(win.begin(), win.end(), cmp1);
int temp = (win[0].hh - input[i].h) * 3600 + (win[0].mm - input[i].m) * 60 + win[0].ss - input[i].s;
if (temp > 0)
{
total += temp;
win[0].hh = win[0].hh + (win[0].mm + input[i].time) / 60;
win[0].mm = (win[0].mm + input[i].time) % 60;
}
else
{
win[0].hh = input[i].h + (input[i].m + input[i].time) / 60;
win[0].mm = (input[i].m + input[i].time) % 60;
}
cnt++;
}
else
break;
}

printf("%.1lf", (double)(total / (cnt * 60.0)));

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT 数据结构 算法