PAT 甲级 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.

输入

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.

输出

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.

思路

模拟题,稍微麻烦点,大概算法和1014题很像.我们可以对每一个窗口采取队列进行处理,这里也可以简化,只需要存下每一个队列的头即可(num数组)num[i]为第i个窗口当前还需要到多少时间这个客户才会走,因此我们每一轮选取一个num最小的一个窗口,让时间走到这个时候,如果这个时候 n u m m i n num_{min} nummin小于下一个客户来的时间,直接让 n u m m i n = c o s t o m e r i + s p e n d t i m e num_{min}=costomer_i+spendtime nummin=costomeri+spendtime。如果大于的话,则这个客户等待的时间即为两者相减。

代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
typedef struct node {
    int number;
    int time;
    int spend;
    int wait = { 0 };
}Per;
Per per[10010];
int N, K;
queue<int> q;
int num[10005];
void de()
{
    for (int i = 0; i < K; i++)
    {
        if (num[i] != -1)
        {
            per[num[i]].spend--;
            if (per[num[i]].spend == 0)
            {
                num[i] = -1;
            }
        }
    }
}
/*int go(int a)
{
    if(num.size()<n)
    return 0;
}*/
bool cmp(Per a, Per b)
{
    return a.time < b.time;
}
int main()
{

    scanf("%d%d", &N, &K);
    int all = 0;
    for (int i = 0; i < N; i++)
    {
        int h, m, s,sp;
        scanf("%d:%d:%d %d", &h, &m, &s, &sp);
        int t = h * 3600 + m * 60 + s;
        if (t <= 61200)
        {
            per[all].time = t;
            per[all].spend = sp * 60;
            all++;
        }
    }
    if (all == 0)
    {
        printf("0.0\n");
        return 0;
    }
    sort(per, per + all, cmp);
    for (int i = 0; i <= K; i++)
    { 
        num[i] = 28800;
    }
    num[K] = K;
    for (int i = 0; i < all; i++)
    {
        if (per[i].time < 28800)
        {
            per[i].wait += 28800 - per[i].time;
            per[i].time = 28800;
        }
    }
    int cur = 0;
    for (int i = 0; i < all; i++)
    {
        int mmin =num[0];
        int mmin_in = 0;
        for (int j = 0; j < K; j++)
        {
            if (mmin > num[j])
            {
                mmin_in = j;
                mmin = num[j];
            }
        }
        if (per[i].time < num[mmin_in])
        {
            per[i].wait += num[mmin_in] - per[i].time;
            num[mmin_in] += per[i].spend;
        }
        else
        {
            num[mmin_in] = per[i].time+per[i].spend;
        }
    }
    int all_time = 0;
    for (int i = 0; i < all; i++)
    {
        all_time += per[i].wait;
    }
    printf("%.1f\n", (all_time*1.0/60.0)/(all*1.0));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值