1095 Cars on Campus——PAT甲级真题
1095 Cars on Campus
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period.
Input Specification:
Each input file contains one test case. Each case starts with two positive integers N (<= 10000), the number of records, and K (<= 80000) the number of queries. Then N lines follow, each gives a record in the format
plate_number hh:mm:ss status
where plate_number is a string of 7 English capital letters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59; and status is either in or out.
Note that all times will be within a single day. Each “in” record is paired with the chronologically next record for the same car provided it is an “out” record. Any “in” records that are not paired with an “out” record are ignored, as are “out” records not paired with an “in” record. It is guaranteed that at least one car is well paired in the input, and no car is both “in” and “out” at the same moment. Times are recorded using a 24-hour clock.
Then K lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given in ascending order of the times.
Output Specification:
For each query, output in a line the total number of cars parking on campus. The last line of output is supposed to give the plate number of the car that has parked for the longest time period, and the corresponding time length. If such a car is not unique, then output all of their plate numbers in a line in alphabetical order, separated by a space.
Sample Input:
16 7
JH007BD 18:00:01 in
ZD00001 11:30:08 out
DB8888A 13:00:00 out
ZA3Q625 23:59:50 out
ZA133CH 10:23:00 in
ZD00001 04:09:59 in
JH007BD 05:09:59 in
ZA3Q625 11:42:01 out
JH007BD 05:10:33 in
ZA3Q625 06:30:50 in
JH007BD 12:23:42 out
ZA3Q625 23:55:00 in
JH007BD 12:24:23 out
ZA133CH 17:11:22 out
JH007BD 18:07:01 out
DB8888A 06:30:50 in
05:10:00
06:30:50
11:00:00
12:23:42
14:00:00
18:00:00
23:59:00
Sample Output:
1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09
题目大意:
让你模拟车库的停车过程。首先每辆车都有一个车牌号,和对应的时间和状态,如果状态是in表示入库,如果状态是Out表示出库。出库时间一定紧跟在入库时间之后,只有出库和入库像配对的才算做合法时间。给出K组查询,查询时间按照由小到大一次递增。每组查询要求你给出相应时间段内在停车库的车辆数。最终给出待在停车库内时间最长的车的车牌号以及相应时间。
大致思路
- 定义一个结构体用来存储各个车辆的信息。同时定义一个map用来建立车牌号和车辆信息之间的映射关系,方便后续对每辆车的进出时间进行排序。
- 对每一辆车的进出时间进行排序,同时按照题目要求判断合法的时间段,因为答案要求的是每辆车待在车库的最长时间,所以我们定义一个map<string, int> record用来记录每辆车待在车库里的时间,同时定义一个vector< pair<int, int> > during;用来记录每辆车的进入和离开时间。建立一个vector maxTime用来存储待在车库里最长时间的车牌号。
- 在查询时,把查询时间和during中的合法时间进行比较,如果查询时间大于进入时间表明有车进入如果查询时间小于进入时间表明有车离开。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
string id, status;
int hh, mm, ss; //时,分,秒
bool friend operator<(node a, node b) {
if (a.hh == b.hh) {
if (a.mm == b.mm) return a.ss < b.ss;
return a.mm < b.mm;
}
return a.hh < b.hh;
}
};
int n, k;
// typedef pair<int, int> P;
vector< pair<int, int> > during;
vector<string> maxTime;
int main() {
scanf("%d%d", &n, &k);
map<string, vector<node>> car;
map<string, int> record; //记录每辆车的进入时间和最大时间
for (int i = 0; i < n; i++) {
node tmp;
cin >> tmp.id;
scanf("%d:%d:%d", &tmp.hh, &tmp.mm, &tmp.ss);
cin >> tmp.status;
car[tmp.id].push_back(tmp);
}
//对每一辆车出入时间进行排序
for (auto ite : car) {
auto tmp = ite.second;
sort(tmp.begin(), tmp.end());
int len = tmp.size();
for (int i = 0; i < len - 1; i++) {
if (tmp[i].status == "in" && tmp[i + 1].status == "out") {
int in_time = tmp[i].hh * 3600 + tmp[i].mm * 60 + tmp[i].ss;
int out_time =
tmp[i + 1].hh * 3600 + tmp[i + 1].mm * 60 + tmp[i + 1].ss;
int stay_time = out_time - in_time;
record[tmp[i].id] += stay_time; //记录每辆车待的最长时间
during.push_back(make_pair(
in_time, out_time)); // during记录每一辆车的进来和出去时间
}
}
if (maxTime.empty())
maxTime.push_back(ite.first);
else {
if (record[ite.first] == record[maxTime[0]])
maxTime.push_back(ite.first);
else if (record[ite.first] > record[maxTime[0]]) {
maxTime.clear();
maxTime.push_back(ite.first);
}
}
}
while (k--) {
int call_hh, call_mm, call_ss;
scanf("%d:%d:%d", &call_hh, &call_mm, &call_ss);
int last_time = call_hh * 3600 + call_mm * 60 + call_ss;
int ans = 0;
for (int i = 0; i < during.size(); i++) {
if (last_time >= during[i].first) ans++;
if (last_time >= during[i].second) ans--;
}
printf("%d\n", ans);
}
sort(maxTime.begin(), maxTime.end());
for (int i = 0; i < maxTime.size(); i++) cout << maxTime[i] << " ";
// cout << "最长时间为:" << record[maxTime[0]] << endl;
int ans_hh = record[maxTime[0]] / 3600;
record[maxTime[0]] %= 3600;
int ans_mm = record[maxTime[0]] / 60;
int ans_ss = record[maxTime[0]] % 60;
printf("%02d:%02d:%02d\n", ans_hh, ans_mm, ans_ss);
return 0;
}
运行结果如下图所示:

1095 Cars on Campus——PAT甲级真题的更多相关文章
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
- Count PAT's (25) PAT甲级真题
题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- 1022 Digital Library——PAT甲级真题
1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...
随机推荐
- Kubernetes --(k8s)volume 数据管理
容器的磁盘的生命周期是短暂的,这就带来了许多问题:第一:当一个容器损坏了,kubelet会重启这个容器,但是数据会随着container的死亡而丢失:第二:当很多容器在同一Pod中运行的时候,经常需要 ...
- 用hyper-v创建虚拟机
1.新建虚拟机 1) 2) 3) 4)一般情况:linux选择第一代,Windows选择第二代 5) 6) 7) 8) 9) 10) 11)网卡设置:如果虚拟机和宿主机公用一块网卡,那么VLAN ID ...
- OSPF总结
参考文档:OSPF知识点总结(华为)https://wenku.baidu.com/view/8cc8ab52a36925c52cc58bd63186bceb19e8edf6.html OSPF概念 ...
- 深入理解Java虚拟机读书笔记 -- Java内存区域
Graal VM: Run Programs Faster Anywhere. 跨语言全栈虚拟机,可以作为"任何语言"的运行平台使用. Java内存结构 程序计数器:线程私有,较小 ...
- kafka的演进历史
首先如果我开始做一个消息队列,最开始的时候可能就是一台单机上的一个单一的log日志,不断地向这个日志中追加消息即可. 后来,可能由于一个log日志支撑不了太多的读写请求,于是就对这个log日志进行了拆 ...
- charles(3)charles防止30分钟自动重启
前言 Charles是收费软件,可以免费试用30天.试用期过后,未付费的用户仍然可以继续使用,但是每次使用时间不能超过30分钟,并且启动时将会有10秒种的延时. 此时,我们只需网上找一个注册码即可 解 ...
- linux(2)系统目录结构
前言 平常linux系统用的也不少,那么linux下的每个目录都是用来干什么的,小伙伴们有仔细研究过吗?让我们来了解下吧 Linux 系统目录结构 登录系统后,在当前命令窗口下输入命令: [root@ ...
- CTGU_训练实录
前言 之前做题都没有感觉,慢慢出去比赛后,打Codeforces,看别的人博客,知乎上的讨论,慢慢的对算法有一些自己的思考.特写是最近看知乎上别人说的Dijkstra利用水流去理解,LCA的学习,感觉 ...
- codeforces632E. Thief in a Shop (dp)
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
- MySQL8.0数据库出现的问题——外码创建方式、外键约束两个引用列不兼容问题、check约束问题、用触发器代替check约束、关键字DELIMITER、删除添加索引、删除添加外键约束、和一些数据库方面的操作
一.首先先说一下我们都需要建立那些表 mysql> CREATE TABLE IF NOT EXISTS `student`( -> `sno` CHAR(8) NOT NULL, -&g ...