模拟
以前老师说,模拟是最考验代码水平的(笑),确实啊。
这题和前面一题有点像,换了个不同的做法,模拟了每一秒时的情况,等待时间就是每秒钟排队的人数。
注意点就是是否服务取决于客户到来的时间,和当前时间是否超过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;
}