【PAT甲级】 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 (≤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
思路
题目要求
给定n个客户,k个窗口;给出用户的到达时间和需要的服务时间;一个人的服务时长不得多于一小时;算出平均等待时间。
题目分析
- 需要注意的是八点前到的得等到八点才可以开始;17点后到的无法进行服务;
- 用户有到达时间和需要的服务时间,将用户看作一个整体(结构体);
- 将用户按照到达顺序排序;
- 由于时间精确到秒,故可以把时间均转换成秒进行计算;
- 窗口有当前服务结束时间,此时从用户队列中取出最先到达的;若达到时间早于结束时间,则等待时间=结束时间-到达时间;否则直接更新结束时间即可;
参考代码如下
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct customer{
int intime;
int sertime;
}customers[10001];
bool mycmp(customer a,customer b){
return a.intime < b.intime;
}
int main() {
int n,k;
scanf("%d %d",&n,&k);
int num=0;
for(int i=0;i<n;i++){
int hour,minute,second,time;
scanf("%d:%d:%d %d",&hour,&minute,&second,&time);
second=(hour*60+minute)*60+second;
if(second > 61200) continue;
customers[num].intime=second;
customers[num].sertime=time*60;
num++;
}
sort(customers,customers+num,mycmp);
int index=0;
priority_queue<int, vector<int>, greater<int> > q;
for(int i=0;i<k;i++){
q.push(28800);//都变成八点
}
int sum=0;
while(index<num){
if(customers[index].intime<q.top()){
sum+=(q.top()-customers[index].intime);
q.push(q.top()+customers[index].sertime);
}
else{
q.push(customers[index].intime+customers[index].sertime);
}
q.pop();
index++;
}
if(num){
printf("%.1lf",((double)sum/60.0)/((double)num));
}
else printf("0.0");
}
附
- 网上的参考代码很多;
- 以上仅是用来记录我个人的想法和答题方式;
- 仅为入门选手,如有错误或者更好的方法欢迎指正与探讨;