1017 Queueing at Bank (25)(25 分)

版权声明:实不相瞒,我也想成为大佬 https://blog.csdn.net/CV_Jason/article/details/81367895


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 (<=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 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

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805491530579968

2 解题思路

  这道题与1014属于一个类型,都是队列问题。除了问的结果不一样,其余几乎没啥太大的差别,可以用类似的思路去解决。
  先把客户的时间换算成秒钟,这样方便计算和排序,输入所有的时间之后,按照总秒数进行排序,大于17*60*60的跳过(超过17点,下班了)。输入有效客户之后,开始分配客户。分配客户到窗口的时候只有两种情况:
  1. 客户来的时候窗口有没有人的情况,此时等待时间为0,刷新窗口的结束时间;
  2. 客户来的时候,窗口都有人,那么循环比较等待时间最小的一个,刷新窗口的结束时间;

  最后计算等待时间总和除以有效客户的数量即可,注意有效客户为0的边界情况

3 AC代码

/*
** @Brief:No.1017 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-7-31 
*/

#include<iostream>
#include<algorithm>
#include<cmath>

#define EARLIEST 60*60*8
#define LAST 60*60*17

using namespace std;

struct customer{
    int time;
    int process_time;
};

bool cmp(customer a,customer b){
    return a.time<b.time;
}

customer ccc[10001];
int windows[101];

int main(){
    int N,K;
    int index = 0;
    scanf("%d%d",&N,&K);
    for(int i=0;i<N;i++){
        int hour;
        int minute;
        int second;
        int time;
        int process_time;
        scanf("%d:%d:%d %d",&hour,&minute,&second,&process_time);
        time = hour*3600+minute*60+second;
        if(time>LAST){
            continue;
        }
        ccc[index].time = time;
        ccc[index++].process_time = process_time*60;
    }
    fill(windows,windows+K,EARLIEST);
    if(index==0){
        printf("0.0\n");
        return 0;
    }   
    sort(ccc,ccc+index,cmp);    
    float sum_time = 0.0f;
    for(int i=0;i<index;i++){
        int select_window = 0;
        int wait_time = LAST;
        for(int j=0;j<K;j++){
            if(ccc[i].time>windows[j]){
                wait_time = 0;
                select_window = j;
                windows[j] = ccc[i].time;
                break;
            }else if(wait_time>windows[j]-ccc[i].time){
                wait_time = windows[j]-ccc[i].time;
                select_window = j;
            }
        }
        sum_time +=wait_time;
        windows[select_window] +=ccc[i].process_time; 
    }
    printf("%.1f\n",sum_time/(index*60.0));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CV_Jason/article/details/81367895