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 (≤ 1 0 4 10^4 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

翻译

本题计算顾客平均等待时间

代码

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int n,k;
//n-total number of customers
//k-the number of windows
struct customer
{
    int come_time,process_time,leave_time=0;
};
customer c[10001];
bool cmp(customer a,customer b)
{
    return a.come_time<b.come_time;
}
int main()
{
    cin>>n>>k;
    int i,j;
    for(int i=0;i<n;i++)
    {
        int hh,mm,ss,process_time;
        char d;
        cin>>hh>>d>>mm>>d>>ss>>process_time;
        c[i].come_time=hh*3600+mm*60+ss;
        c[i].process_time=process_time*60;
        if(c[i].process_time>3600) c[i].process_time=3600;
    }
    c[n].come_time=100000;//比61200大即可
    sort(c,c+n,cmp);
    int window[k];
    int next=0;
    for(i=0;i<k;i++) window[i]=-1;//-1表示窗口空闲
    double sum=0;
    for(int time=28800;c[next].come_time<=61200;time++)
    {
        //送客
        for(i=0;i<k;i++)
        {
            if(window[i]>=0)
            {
                j=window[i];
                if(c[j].leave_time==time)
                {
                    window[i]=-1;
                }
            }
        }
        //入队:当窗口空闲并且顾客来的时间小于等于当前时间
        for(i=0;i<k;i++)
        {
            if(window[i]==-1)
            {
                if(c[next].come_time<=61200 && c[next].come_time<=time)
                {
                    window[i]=next;
                    next++;
                }
            }
        }
        //迎客:当窗口空闲并且是新的顾客时,计算该顾客的离开时间
        for(i=0;i<k;i++)
        {
            if(window[i]>=0)
            {
                j=window[i];
                if(c[j].leave_time==0)
                {
                    sum += (time-c[j].come_time);
                    c[j].leave_time=time+c[j].process_time;
                }
            }
        }
    }
    printf("%.1f",sum/next/60);
    system("pause");
}

思路

很久没有更新刷题笔记了,最近准备推免机试(虽然不知道有没有机试)。
进入正题!
首先这是一道排队问题,和1014很像,可以套用前面的思维方式,改变某些细节。
常用的将时间转换为最小单位秒,再对来的贵客入场时间排个序,方便后面的循环。
我将处理过程分为三步:送客、入队和迎客
在此之前,说一下循环条件的不同,因为题意是来的顾客只要在17:00来了,银行是一定会处理完的,所以终止条件是入场时间晚于17:00.
送客:遍历所有窗口,如果这个窗口的有人并且他的离开时间正好是当前时间,好,走人
入队:遍历所有窗口,如果这个窗口没有并且顾客来的时间小于等于当前时间,好,入队
迎客:当窗口空闲并且是新的顾客时,计算该顾客的离开时间,好,准备走人
最后是计算等待时间,送客是不一定全部都可以遍历到的,比方说有10个顾客,可能他的离开时间已经晚于17:00,所以可以在入队和迎客循环中进行累计。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值