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

PAT 甲级 1017 Queueing at Bank (25分)

2020-07-03 14:15 127 查看

题目

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 Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤104) - 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

题目大意

银行工作的时间为8:00-17:00,8:00:00之前到达的顾客需要等待,17:00:01及之后的顾客不予接待;
给出N个顾客到达时间和需要花费的时间,计算被服务顾客的平均等待时间。

思路

以秒为单位记录所有时间方便比较,因为只有时间上的比较所以可以用优先队列;对每个顾客对情况,对记录对所有顾客首先按到达时间进行排序;每次从优先队列得到最早结束时间,每次遍历到对顾客到达时间与firstend进行比较:

  • 如果到达时间比结束时间晚,那么重新加入优先队列的时间为到达时间+需要花费时间
  • 反之,重新加入优先队列的时间为结束时间+需要花费时间

代码

#include<bits/stdc++.h>
using namespace std;
struct person{
int arrive, cost;
};
bool cmp(const person& a, const person& b){ return a.arrive < b.arrive; }
int main(int argc, const char * argv[]) {
int N, K, hh, mm, ss, c, cnt=0, wait=0;
int start = 3600*8, end = 17*3600+1;
cin>>N>>K;
vector<person> v;
for(int i=0; i<N; i++){
scanf("%d:%d:%d %d",&hh, &mm, &ss, &c);
v.push_back({ hh*3600 + mm*60 + ss, c*60 });
}
sort(v.begin(), v.end(), cmp);
priority_queue<int, vector<int>, greater<int> > prique; // 小顶堆
for(int i=0; i<K; i++)
prique.push(start);
for(auto it : v){
if(it.arrive < end){
cnt++;
int tmp = prique.top();
prique.pop();
if(tmp > it.arrive) {
wait += tmp - it.arrive;
prique.push(tmp + it.cost);
}else
prique.push(it.arrive + it.cost);

}
}
if(cnt == 0) printf("0");
else printf("%.1lf", wait/(60.0*cnt));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: