浙大 PAT 甲级 1017 Queueing at Bank C++
思路
跟1014题很像,也是一道模拟排队题。区别在于1014侧重单入口多出口的排队,默认所有人都在开始时刻开始排队,而1017题黄线前的若干条队伍每队只能容纳1人,并且考虑不同时刻的到达。
数据结构考虑一个结构体Customer代表排队的人,两个成员变量:到达时间、业务办理所需时长。用一个vector<Custmoer>代表队列,sort函数(#include<algorithm>)使其按照到达时间排序即可。还有一个整型数组,下标代表窗口编号,其值代表每个窗口恢复空闲的时刻,最开始为八点,过程中为占用这个窗口的用户的到达时间加上其业务所需时长。
以上是大致思路,还有两个细节需要考虑:
- 17:00及以后到达的,不能加入vector,直接忽略即可。
- 8:00以前到达的,如果把算法写的具有普遍性,那么这一点就完全不需要考虑。我的做法是,定义一个标记变量bool nowait,把客户分成经历了等待时间的,和未经历等待时间的,分别处理,这样算法就可能会简单一些。
代码
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;struct Customer
{int atime;int ptime;
};
vector<Customer> lines;
int windows[100];bool cmp(Customer a, Customer b)
{return a.atime < b.atime;
}int main()
{int N, K;scanf("%d%d", &N, &K);for (int i = 0; i < N; i++){int h, m, s, p;scanf("%d:%d:%d%d", &h, &m, &s, &p);Customer c;c.atime = h * 3600 + m * 60 + s;c.ptime = p;if (c.atime < 17 * 3600){lines.push_back(c);}}for (int i = 0; i < K; i++){windows[i] = 8 * 3600;}sort(lines.begin(), lines.end(), cmp);float sum = 0;for (int i = 0; i < lines.size(); i++){bool nowait = false;int min = 10000000;int w = -1;for (int j = 0; j < K; j++){if (windows[j] < lines[i].atime){windows[j] = lines[i].atime + lines[i].ptime * 60;nowait = true;break;}if (windows[j] < min){min = windows[j];w = j;}}if (!nowait){sum += windows[w] - lines[i].atime;windows[w] += lines[i].ptime * 60;}}printf("%.1f", sum / 60.0 / (float)lines.size());return 0;
}
浙大 PAT 甲级 1017 Queueing at Bank C++相关推荐
- PAT甲级1017 Queueing at Bank:[C++题解]字符串、结构体、最小堆
文章目录 题目分析 题目链接 题目分析 客户数据用什么存呢? 好吧,还是用结构体. 结构体里面存什么呢? 到达时间 和服务时间. 窗口怎么存呢? 将窗口的开始服务时间从小到大存,自然想到小根堆. pr ...
- PAT甲级 1017 Queueing at Bank
原题传送门 >>> 几个注意点: 1.凡是在17:00:00之前(包括17:00:00)到的顾客,银行都必须把它们完全服务完成.(也就是说,在这之前银行不下班). 这个设定也太迷惑了 ...
- 浙大PAT甲级-1017
银行队列: (却没用到队列) #include <iostream> #include <string> #include <algorithm> #include ...
- PAT 1017 Queueing at Bank[一般]
1017 Queueing at Bank (25)(25 分)提问 Suppose a bank has K windows open for service. There is a yellow ...
- 浙大PAT甲级1040
浙大PAT甲级1040 原题 问题分析 原题 1040 Longest Symmetric String (25 分) Given a string, you are supposed to outp ...
- 1017 Queueing at Bank (25 分)_27行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Suppose a bank has K windows open for service. There is a yellow ...
- 【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 wh ...
- PAT (Advanced Level) 1017 Queueing at Bank(模拟)
题目链接:点击查看 题目大意:模拟银行服务的过程,输出每个客户的平均等待时间 题目分析:类似的银行服务模拟题,不过与之前那个题不太一样的是,这一次所需要统计的信息变少了,只需要统计一下每个客户的平均等 ...
- PAT (Advanced Level) 1017. Queueing at Bank (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- 浙大PAT甲级1019. General Palindromic Number (20)
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
最新文章
- 官方全面解读“5G+工业互联网”
- About JXTA message reliable design
- TF之LSTM:基于tensorflow框架自定义LSTM算法实现股票历史(1990~2015数据集,6112预测后100+单变量最高)行情回归预测
- 天勤数据结构:树与二叉树(图解二叉树的三种遍历方式执行流程,超详细)
- defer func(参数){}
- WebView 访问 url asset sd 网页
- 判断Json字符串返回类型 对象 或者 数组
- [Swift实际操作]七、常见概念-(12)使用DispatchGroup(调度组)管理线程数组
- Facebook如何“养号”干货分享
- GL和DX中关于纹理操作(包括多重纹理)与混合的问题
- Jupyter Notebook安装jupyter_contrib_nbextension扩展功能和安装后不显示Nbextensions标签的解决办法
- Facebook对MySQL全表扫描性能的改进
- 函数响应式编程及ReactiveObjC学习笔记 (三)
- 求两个数最小公倍数的7种方法
- 字体反爬案例解析:大众点评
- (01)开发环境准备
- kindeditor=4.1.5上传漏洞复现
- CSS 样式的 initial(默认)和 inherit(继承)以及 unset
- 《逆赛博格教堂与集市》——国内首个基于区块链的人与AI协作艺术展即将开幕...
- DDR4硬件原理图设计详解