PAT甲级——1095 Cars on Campus (排序、映射、字符串操作、题意理解)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93135047
Zhejiang University has 8 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 (≤), the number of records, and K (≤) 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
题目大意:记录校园车辆的出入信息,根据查询时间输出当前校园内停放车辆的数量,最后输出停留时间最长的车辆信息和最长停留时间,如果有多辆车停留时间相同,按照字母续排列。
题意理解:题目的坑点在于同一辆车出入时间的配对,存在无用时间需要忽略。正确的配对要求是相邻最近的出入时间,所以需要对每辆车的出入时间保存完进行排序寻找最相近的出入时间,其他的就是字符串转换、映射、排序之类的基本操作了,直接使用系统函数和容器。
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std; struct timeNode {
vector<int> inTime, outTime;
};
struct carNode {
string plate;
int time;
}; int toSecond(string& s);//把时间转换成秒,便于计算
void toStr(int n, string& s, bool flag);//用于时、分、秒的转换
bool cmp(const carNode& a, const carNode& b);
string toHMS(int time);//把秒转成规定格式的时间 unordered_map<string, timeNode> mp;//记录每辆车的原始出入时间信息
vector<carNode> carInfo;//记录每辆车的停留时间长度
vector<int> carIn, carOut;//记录配对完成的正确的车辆出入时间 int main()
{
int N, K;
string plate, strTime;
scanf("%d%d", &N, &K);
for (int i = ; i < N; i++) {
string flag;
cin >> plate >> strTime >> flag;
if (flag == "in")
mp[plate].inTime.push_back(toSecond(strTime));
else
mp[plate].outTime.push_back(toSecond(strTime));
}
for (auto it = mp.begin(); it != mp.end(); it++) {
int i = , j = , time = ,
inTimeSize = it->second.inTime.size(),
outTimeSize = it->second.outTime.size();
sort(it->second.inTime.begin(), it->second.inTime.end());
sort(it->second.outTime.begin(), it->second.outTime.end());
/*将驶入驶出时间进行配对*/
while (i < inTimeSize && j < outTimeSize) {
if (it->second.inTime[i] >= it->second.outTime[j]) {
while (j < outTimeSize && it->second.inTime[i] >= it->second.outTime[j]) { j++; }
}
else {
while (i < inTimeSize && it->second.inTime[i] < it->second.outTime[j]) { i++; }
if (i != )
i--;
carIn.push_back(it->second.inTime[i]);
carOut.push_back(it->second.outTime[j]);
time += it->second.outTime[j] - it->second.inTime[i];
i++;
j++;
}
}
/*保存每辆车的停留总时间*/
carInfo.push_back({ it->first,time });
}
/*根据查询时间输出当前校园内车辆的数量*/
sort(carIn.begin(), carIn.end());
sort(carOut.begin(), carOut.end());
int inIndex = , outIndex = ;
for (int i = ; i < K; i++) {
cin >> strTime;
int tmpTime = toSecond(strTime);
while (inIndex < carIn.size() && carIn[inIndex] <= tmpTime) { inIndex++; }
while (outIndex < carOut.size() && carOut[outIndex] <= tmpTime) { outIndex++; }
printf("%d\n", inIndex - outIndex);
}
/*输出停留时间最长的车辆牌照*/
sort(carInfo.begin(), carInfo.end(), cmp);
for (int i = ; i < carInfo.size() && carInfo[i].time == carInfo[].time; i++) {
cout << carInfo[i].plate << " ";
}
cout << toHMS(carInfo[].time) << endl;//将最大停留时间转换成标准格式输出
return ;
} bool cmp(const carNode& a, const carNode& b) {
return a.time != b.time ? a.time > b.time:a.plate < b.plate;
} void toStr(int n, string& s, bool flag) {
if (n >= ) {
s.push_back(n / + '');
s.push_back(n % + '');
}
else {
s.push_back('');
s.push_back(n + '');
}
if (flag)
s.push_back(':');
} string toHMS(int time) {
string HMS;
toStr(time / , HMS, true);
toStr(time % / , HMS, true);
toStr(time % % , HMS, false);
return HMS;
} int toSecond(string& s) {
int hour = (s[] - '') * + s[] - '',
minute = (s[] - '') * + s[] - '',
second = (s[] - '') * + s[] - '';
return hour * + minute * + second;
}
PAT甲级——1095 Cars on Campus (排序、映射、字符串操作、题意理解)的更多相关文章
- PAT甲级1095. Cars on Campus
PAT甲级1095. Cars on Campus 题意: 浙江大学有6个校区和很多门.从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码.现在有了所有的信息,你应该在任何特定的时间点告诉在 ...
- PAT甲级——A1095 Cars on Campus
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- 1095 Cars on Campus——PAT甲级真题
1095 Cars on Campus Zhejiang University has 6 campuses and a lot of gates. From each gate we can col ...
- PAT 1095 Cars on Campus
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...
- 【刷题-PAT】A1095 Cars on Campus (30 分)
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...
- 【PAT甲级】1095 Cars on Campus (30 分)
题意:输入两个正整数N和K(N<=1e4,K<=8e4),接着输入N行数据每行包括三个字符串表示车牌号,当前时间,进入或离开的状态.接着输入K次询问,输出当下停留在学校里的车辆数量.最后一 ...
- PAT (Advanced Level) Practise - 1095. Cars on Campus (30)
http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...
- PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)
题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...
- PAT (Advanced Level) 1095. Cars on Campus (30)
模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...
随机推荐
- Android基于socket的群聊程序
在网上看了好多,但是感觉不是太简单就是只能单独聊,所以就自己写了个可以群聊的,直接上代码了 一.服务器端 这里用的MyEclipse作为服务器端 MyServerScoket.java package ...
- linux下导入导出oracle的dmp文件
1.导出dmp件 命令:exp QGTG/\"QGTG@orcl\" file=/usr/fuck.dmp exp QGTG/\"QGTG@orcl\" fil ...
- Codechef Union on Tree
Codechef Union on Tree https://www.codechef.com/problems/BTREE 简要题意: 给你一棵树,\(Q\)次询问,每次给出一个点集和每个点的\(r ...
- 使用Rancher搭建K8S测试环境
使用Rancher搭建K8S测试环境 http://blog.csdn.net/csdn_duomaomao/article/details/75316926 环境准备(4台主机,Ubuntu16.0 ...
- mysql root密码忘记重置
1.修改/etc/my.cnf文件 找到mysqld选项,增加子项skip-grant-tables 2.重新启动mysql服务 service mysqld restart 3.进入mysql 在s ...
- HDOJ(1018)
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 报错之-Cannot set property 'onclick' of null
当js文件放在head里面时,如果绑定了onclick或者onmouseover事件,就会出现如下图类似的错误,是因为浏览器的加载你写的html文档的顺序是从上往下,加载完按钮节点才执行的js,所以当 ...
- ssh-keygen和ssh-copy-id的简单使用
实验环境是CentOS7: ssh-keygen产生公钥和私钥对. ssh-copy-id:将本机的公钥使用ssh协议复制到远程的客户端,ssh协议的公钥和私钥一般存放于~/.ssh下 #主机 [ro ...
- mariadb的读写分离
实验环境:CentOS7 设备:一台主数据库服务器,两台从数据库服务器,一台调度器 主从的数据库配置请查阅:http://www.cnblogs.com/wzhuo/p/7171757.html : ...
- python 链表
在C/C++中,通常采用“指针+结构体”来实现链表:而在Python中,则可以采用“引用+类”来实现链表. 节点类: class Node: def __init__(self, data): sel ...