当前位置: 代码迷 >> 综合 >> PAT甲级-1017 Queueing at Bank (25分)
  详细解决方案

PAT甲级-1017 Queueing at Bank (25分)

热度:105   发布时间:2023-09-26 23:54:26.0

点击链接PAT甲级-AC全解汇总

题目:
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:

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

题意:
银行八点开门,下午五点关门,关门之前所有来的人都会处理!!那怕都已经到第二天了,照样会处理完!最后一个case的考点!
输入的是到达时间和需要处理的时间,计算所有人等待的平均值。
没有考察0人的情况,所以输出的时候没必要判断cnt是否为0,不过最好还是写一下养成习惯。

思路是把所有时间用秒表示,这样方便多了。本来还以为我真是个机灵鬼,写完查了下其他大佬也都是这样处理的…

不过我有个不理解的地方是为什么输入的时候如果发现这个人超过五点到达直接不计入统计的话,case0 1会错误,这是什么原因呢?好在一开始我是在统计的时候做的判断。

我的代码:

#include<bits/stdc++.h>
using namespace std;class Person{
    
public:Person(int a,int b):arrive_(a),time_need_(b){
    }int arrive_;int time_need_;
};bool cmp(Person a,Person b)
{
    return a.arrive_<b.arrive_;
}int main()
{
    int N,W,sum_wait=0,sum_cnt=0;cin>>N>>W;int widows[W]={
    0};vector<Person>persons;for(int i=0;i<W;i++)widows[i]=8*3600;for(int i=0;i<N;i++){
    int hh,mm,ss,time_need;scanf("%d:%d:%d %d",&hh,&mm,&ss,&time_need);int arrive=hh*3600+mm*60+ss;
// if(arrive>17*3600)continue;//这里判断的话case0 1会报错if(time_need>60)time_need=60;Person t(arrive,time_need*60);persons.push_back(t);}sort(persons.begin(),persons.end(),cmp);for(int i=0;i<N;i++){
    //找窗口时间最小的int min_time=240*3600,index=0;for(int i=0;i<W;i++){
    if(min_time>widows[i]){
    min_time=widows[i];index=i;}}if(persons[i].arrive_>17*3600)continue;if(persons[i].arrive_<min_time)//需要等{
    sum_wait+=min_time-persons[i].arrive_;widows[index]+=persons[i].time_need_;}else{
    widows[index]=persons[i].arrive_+persons[i].time_need_;}sum_cnt++;}printf("%.1f\n",1.0*sum_wait/60/sum_cnt);//case没有考察对0人的情况return 0;
}