PTA 7-5 银行排队问题之单队列多窗口服务

网友投稿 353 2022-11-01


PTA 7-5 银行排队问题之单队列多窗口服务

假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。

本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间,并且统计每个窗口服务了多少名顾客。

#include #include #include #include using namespace std;const int MAXN = 1010;const int INF = 0x3f3f3f3f;struct Data { int x, y;}data[MAXN];struct People { int start, finish;};struct Window { int cnt; People people[MAXN]; Window() { cnt = 0; people[0].start = people[1].start = -1; }};int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &data[i].x, &data[i].y); if (data[i].y > 60) data[i].y = 60; } int m; scanf("%d", &m); Window window[m + 5]; int sumwaittime = 0, maxwaittime = 0, maxfinishtime = 0; for (int i = 1; i <= n; i++) { bool ok = true; int minval = INF, pos = -1; for (int j = 1; j <= m; j++) { Window &w = window[j]; if (w.people[w.cnt].finish <= data[i].x) { int &cnt = window[j].cnt; ++cnt; People &p = window[j].people[cnt]; p.start = data[i].x, p.finish = data[i].x + data[i].y; maxfinishtime = max(maxfinishtime, p.finish); ok = false; break; } if (minval > w.people[w.cnt].finish) { minval = w.people[w.cnt].finish; pos = j; } } if (ok) { int &cnt = window[pos].cnt; ++cnt; People &p = window[pos].people[cnt]; p.start = data[i].x, p.finish = minval + data[i].y; sumwaittime += minval - p.start; maxwaittime = max(maxwaittime, minval - p.start); maxfinishtime = max(maxfinishtime, p.finish); } } printf("%.1lf %d %d\n", 1.0*sumwaittime/n, maxwaittime, maxfinishtime); printf("%d", window[1].cnt); for (int i = 2; i <= m; i++) printf(" %d", window[i].cnt); printf("\n"); return 0;}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PTA 7-2 一元多项式求导
下一篇:Spring Boot 项目启动失败的解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~