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 ...
 
随机推荐
- ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)
			
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...
 - mongodb 学习资料
			
1 入门 http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html http://www.cnblogs.com/hoo ...
 - Maven: 自动远程部署
			
1. 在settings.xml中的Servers节点中增加Server的登录信息: <server> <id>deploy_server_65</id> < ...
 - Linux mount指令
			
-o,是指option,可以指定username,password:当时我们就碰到一个坎,如何来避免输入用户名密码,其实本质并不是避免输入用户名米吗,而是某种可知的方式来进行权限控制:解决的方式就是采 ...
 - JVM体系结构之一:总体介绍
			
一.Java的内存区域划分 Java 虚拟机在执行Java程序的时候会把它管理的内存区域划为几部分,这一节我们就来解析一下Java的内存区域. Java的内存区域主要分为五部分: 程序计数器(PC) ...
 - DTP模型之一:(XA协议之一)XA协议、二阶段2PC、三阶段3PC提交
			
XA协议 XA是一个分布式事务协议,由Tuxedo提出.XA中大致分为两部分:事务管理器和本地资源管理器.其中本地资源管理器往往由数据库实现,比如Oracle.DB2这些商业数据库都实现了XA接口,而 ...
 - Python函数(十二)-迭代器
			
字符串,列表,元组,字典,集合,生成器这些能通过for循环来遍历的数据类型都是可迭代对象 可通过isinstance判断是不是可迭代对象 >>> from collections i ...
 - /*透明度设置的两种方式,以及hover的用法,fixed,(relative,absolute)这两个一起用*/
			
<!DOCTYPE html> /*透明度设置的两种方式,以及hover的用法,fixed,(relative,absolute)这两个一起用*/ <html lang=" ...
 - sharepoint Foundation 2013  error
			
安装必须软件时提示以下错误 错误提示日志: 015-05-28 10:40:25 - Request for install time of 应用程序服务器角色.Web 服务器(IIS)角色2015- ...
 - [51nod1181]质数中的质数(素数筛法)
			
解题关键: 注意下标 #include<bits/stdc++.h> #define maxn 10000002 using namespace std; typedef long lon ...