PAT 甲级 1017 Queueing at Bank

模拟
以前老师说,模拟是最考验代码水平的(笑),确实啊。
这题和前面一题有点像,换了个不同的做法,模拟了每一秒时的情况,等待时间就是每秒钟排队的人数。
注意点就是是否服务取决于客户到来的时间,和当前时间是否超过17点没关系。
回顾了优先队列。

#include <iostream>
#include <cstdio>
#include <algorithm> 
#include <cstring>
#include <cmath>
#include <vector>
#include <queue> 
using namespace std;
struct Cus
{
    long long a_time;
    long long p_time;
};
bool cmp(Cus a, Cus b)
{
    return a.a_time < b.a_time;
}
bool operator < (const Cus &a, const Cus &b)
{
    return a.p_time > b.p_time;
}

int main()
{
    long long n, k;
    Cus c[10005];
    queue<Cus> q;
    priority_queue<Cus> w;
    while (~scanf("%lld %lld", &n, &k))
    {
        while (!q.empty())
        {
            q.pop();
        }
        while (!w.empty())
        {
            w.pop();
        }
        long long hh, mm, ss, p;
        long long st = 8 * 60 * 60;
        long long et = 17 * 60 * 60 + 1;
        long long time = st;
        double sum = 0;
        long long t;
        long long num = 0;
        for (long long i = 0; i < n; i++)
        {
            scanf("%lld:%lld:%lld %lld", &hh, &mm, &ss, &p);
            t = hh * 60 * 60 + mm * 60 + ss;
            if (t < et)
            {
                c[num].a_time = t;
                c[num++].p_time = p * 60;
            }
        }
        sort(c, c + num, cmp);
        long long cnt = 0;
        for (cnt = 0; cnt < num; cnt++)
        {
            if (c[cnt].a_time <= st)
            {
                q.push(c[cnt]);
                sum += st - c[cnt].a_time;
                c[cnt].a_time = st;
            }
            else break;
        }
        queue<Cus> temp;
        while (time < et||!q.empty())
        {
            while (w.size() < k && !q.empty())
            {
                w.push(q.front());
                q.pop();
            }
            while (c[cnt].a_time == time && cnt < num)
            {
                if (w.size() < k)
                {
                    w.push(c[cnt++]);
                }
                else
                {
                    q.push(c[cnt++]);
                }
            }
            sum += q.size();
            time++;
            if (!w.empty())
            {
                while (!w.empty())
                {
                    Cus tt = w.top();
                    tt.p_time--;
                    w.pop();
                    if (tt.p_time != 0)
                    {
                        temp.push(tt);
                    }
                }
                while (!temp.empty())
                {
                    w.push(temp.front());
                    temp.pop();
                }
            }
        }

        printf("%.1lf\n", sum / (60 * num));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值