本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93135047

1095 Cars on Campus (30 分)
 

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 (排序、映射、字符串操作、题意理解)的更多相关文章

  1. PAT甲级1095. Cars on Campus

    PAT甲级1095. Cars on Campus 题意: 浙江大学有6个校区和很多门.从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码.现在有了所有的信息,你应该在任何特定的时间点告诉在 ...

  2. 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 ...

  3. 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 ...

  4. PAT 1095 Cars on Campus

    1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...

  5. 【刷题-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 ...

  6. 【PAT甲级】1095 Cars on Campus (30 分)

    题意:输入两个正整数N和K(N<=1e4,K<=8e4),接着输入N行数据每行包括三个字符串表示车牌号,当前时间,进入或离开的状态.接着输入K次询问,输出当下停留在学校里的车辆数量.最后一 ...

  7. 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 ...

  8. PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)

    题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...

  9. PAT (Advanced Level) 1095. Cars on Campus (30)

    模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...

随机推荐

  1. 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...

  2. [转]_int64、long long 的区别

    大学生程序代写 C++的64位整数[原]by 赤兔 http://baike.baidu.com/view/1235293.htm 在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常 ...

  3. nodejs 上传图片(服务端输出全部代码)

    下面代码,全部都是nodejs端的,不用客户端代码.也就是,选择图片的form表单以及上传完毕预览图片的html,都是由node服务端输出的. 1 启动代码:(node upload.js) var ...

  4. BZOJ2288:[POJ Challenge]生日礼物

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?id ...

  5. 开启现有android项目

    开启工程 在屏幕上方的选单列上,选择「File->New->Project」, 会弹出「New Project」对话视窗.Eclipse 是通用的编辑环境,可根据你所安装的不同扩充套件而支 ...

  6. HDOJ1075字典翻译(map应用)

    #include<iostream> #include<cstdio> #include<map> #include<string> #include& ...

  7. JS---script的位置

    都可以,但各有千秋.放在head中:统一管理,方便维护:但浏览器会首先加载js文件,如果js文件过大,会造成页面在加载js的时候“无反应”时间过长,影响用户体验.放在body中(或放在body后):浏 ...

  8. tar 排除某个目录

    tar -zcvf tomcat.tar.gz --exclude=tomcat/logs --exclude=tomcat/libs tomcat

  9. C/C++中变量类型最值之宏定义

    C/C++ [climits(limits.h)] CHAR_BIT        Number of bits for a char object (byte)                    ...

  10. zookeeper相关知识的总结:

    一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...