本文同步发布在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. 将session存入数据库,memcache的方法

    //存入数据库 <?phpif(!$con = mysql_connect('localhost','root','123456')){    die('连接数据库失败');}$link = m ...

  2. 51Nod1766 树上的最远点对

    1766 树上的最远点对 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i&l ...

  3. ACM学习历程—HDU1023 Train Problem II(递推 && 大数)

    Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know  ...

  4. oracle sql性能

    1.查询所对象相关的表?V$LOCK, V$LOCKED_OBJECT, V$SESSION, V$SQLAREA, V$PROCESS ;查询锁的表的方法:SELECT S.SID SESSION_ ...

  5. 从python2,python3编码问题引伸出的通用编码原理解释

    今天使用python2编码时遇到这样一条异常UnicodeDecodeError: ‘ascii’ code can’t decode byte 0xef 发现是编码问题,但是平常在python3中几 ...

  6. 麻省理工《C内存管理和C++面向对象编程》笔记---第一讲:认识C和内存管理

    最近一年都在用.net和Java,现在需要用C了.昨天看到博客园首页的麻省理工开放课程,就找来看看,正好复习一下.这门<C内存管理和C++面向对象编程>不是那种上来就变量,循环的千篇一律的 ...

  7. Cloudera运维

    1. 增加一个节点 1. 拷贝cm的jar包到该节点 2. 设置hostname(hostnamectl set-hostname XXX),然后修改hosts文件 3. 所有的节点添加该hostna ...

  8. HDU1114(完全背包装满问题)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. SQL语法基础:DDL、DML

    一.DDL(Data Definition Language):数据定义语句 #常见的语句 1)CREATE TABLE/DATABASE:创建数据库 CREATE [TEMPORARY] TABLE ...

  10. java基础知识(2)---语法基础

    二:java语法基础: 1,关键字:其实就是某种语言赋予了特殊含义的单词. 保留字:其实就是还没有赋予特殊含义,但是准备日后要使用过的单词. 2,标示符:其实就是在程序中自定义的名词.比如类名,变量名 ...