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 ...
随机推荐
- Java中的参数传值方式
本文转载自 https://blog.csdn.net/SEU_Calvin/article/details/70089977 1. 你觉得下面程序会输出什么 public static void ...
- PS色调— —通道混合
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); Image=im ...
- bzoj 2342: 双倍回文 回文自动机
题目大意: 定义双倍回文串的左一半和右一半均是回文串的长度为4的倍数的回文串 求一个给定字符串中最长的双倍回文串的长度 题解: 我们知道可以简单地判定以某一点结尾的最长回文串 我们知道可以简单地判定以 ...
- Redo Log File(inactive、active)损坏,处理恢复对策
redolog的生命周期中共有四种状态:current -> 正在使用的active -> 非正在使用的,对应的Dirty Block还没有完全写入到数据文件中inactive -> ...
- Jquery隐藏相同name的div
$("div:[name=divName]").hide(); divName(自己div的Name)
- JavaScript中的BOM知识框架
浏览器对象模型(BOM)以window对象为依托,表示浏览器窗口及可见区域.同时,window对象和还是全局对象,因此所有求安局变量和函数都是它的属性,所有原生框架及其他函数都在它命名之下.BOM中对 ...
- USACO-Greedy Gift Givers(贪婪的送礼者)-Section1.2<2>
[英文原题] Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange ...
- centos6.x禁用ipv6的方法
注意可能有两个网卡的情况,修改当前网卡才有效. cd /etc/sysconfig/network-scripts/ ls ifcfg-Auto_eth0 ifcfg-eth0 现在ipv6没流行,几 ...
- 1、转载 bwa的使用方法
http://bio-bwa.sourceforge.net/bwa.shtml http://www.plob.org/?p=25 bwa的使用需要两中输入文件: Reference genome ...
- 32、Differential Gene Expression using RNA-Seq (Workflow)
转载: https://github.com/twbattaglia/RNAseq-workflow Introduction RNAseq is becoming the one of the mo ...