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
题目大意:记录校园车辆的出入信息,根据查询时间输出当前校园内停放车辆的数量,最后输出停留时间最长的车辆信息和最长停留时间,如果有多辆车停留时间相同,按照字母续排列。
题意理解:题目的坑点在于同一辆车出入时间的配对,存在无用时间需要忽略。正确的配对要求是相邻最近的出入时间,所以需要对每辆车的出入时间保存完进行排序寻找最相近的出入时间,其他的就是字符串转换、映射、排序之类的基本操作了,直接使用系统函数和容器。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <unordered_map> 5 #include <algorithm> 6 using namespace std; 7 8 struct timeNode { 9 vector<int> inTime, outTime; 10 }; 11 struct carNode { 12 string plate; 13 int time; 14 }; 15 16 int toSecond(string& s);//把时间转换成秒,便于计算 17 void toStr(int n, string& s, bool flag);//用于时、分、秒的转换 18 bool cmp(const carNode& a, const carNode& b); 19 string toHMS(int time);//把秒转成规定格式的时间 20 21 unordered_map<string, timeNode> mp;//记录每辆车的原始出入时间信息 22 vector<carNode> carInfo;//记录每辆车的停留时间长度 23 vector<int> carIn, carOut;//记录配对完成的正确的车辆出入时间 24 25 int main() 26 { 27 int N, K; 28 string plate, strTime; 29 scanf("%d%d", &N, &K); 30 for (int i = 0; i < N; i++) { 31 string flag; 32 cin >> plate >> strTime >> flag; 33 if (flag == "in") 34 mp[plate].inTime.push_back(toSecond(strTime)); 35 else 36 mp[plate].outTime.push_back(toSecond(strTime)); 37 } 38 for (auto it = mp.begin(); it != mp.end(); it++) { 39 int i = 0, j = 0, time = 0, 40 inTimeSize = it->second.inTime.size(), 41 outTimeSize = it->second.outTime.size(); 42 sort(it->second.inTime.begin(), it->second.inTime.end()); 43 sort(it->second.outTime.begin(), it->second.outTime.end()); 44 /*将驶入驶出时间进行配对*/ 45 while (i < inTimeSize && j < outTimeSize) { 46 if (it->second.inTime[i] >= it->second.outTime[j]) { 47 while (j < outTimeSize && it->second.inTime[i] >= it->second.outTime[j]) { j++; } 48 } 49 else { 50 while (i < inTimeSize && it->second.inTime[i] < it->second.outTime[j]) { i++; } 51 if (i != 0) 52 i--; 53 carIn.push_back(it->second.inTime[i]); 54 carOut.push_back(it->second.outTime[j]); 55 time += it->second.outTime[j] - it->second.inTime[i]; 56 i++; 57 j++; 58 } 59 } 60 /*保存每辆车的停留总时间*/ 61 carInfo.push_back({ it->first,time }); 62 } 63 /*根据查询时间输出当前校园内车辆的数量*/ 64 sort(carIn.begin(), carIn.end()); 65 sort(carOut.begin(), carOut.end()); 66 int inIndex = 0, outIndex = 0; 67 for (int i = 0; i < K; i++) { 68 cin >> strTime; 69 int tmpTime = toSecond(strTime); 70 while (inIndex < carIn.size() && carIn[inIndex] <= tmpTime) { inIndex++; } 71 while (outIndex < carOut.size() && carOut[outIndex] <= tmpTime) { outIndex++; } 72 printf("%d\n", inIndex - outIndex); 73 } 74 /*输出停留时间最长的车辆牌照*/ 75 sort(carInfo.begin(), carInfo.end(), cmp); 76 for (int i = 0; i < carInfo.size() && carInfo[i].time == carInfo[0].time; i++) { 77 cout << carInfo[i].plate << " "; 78 } 79 cout << toHMS(carInfo[0].time) << endl;//将最大停留时间转换成标准格式输出 80 return 0; 81 } 82 83 bool cmp(const carNode& a, const carNode& b) { 84 return a.time != b.time ? a.time > b.time:a.plate < b.plate; 85 } 86 87 void toStr(int n, string& s, bool flag) { 88 if (n >= 10) { 89 s.push_back(n / 10 + '0'); 90 s.push_back(n % 10 + '0'); 91 } 92 else { 93 s.push_back('0'); 94 s.push_back(n + '0'); 95 } 96 if (flag) 97 s.push_back(':'); 98 } 99 100 string toHMS(int time) { 101 string HMS; 102 toStr(time / 3600, HMS, true); 103 toStr(time % 3600 / 60, HMS, true); 104 toStr(time % 3600 % 60, HMS, false); 105 return HMS; 106 } 107 108 int toSecond(string& s) { 109 int hour = (s[0] - '0') * 10 + s[1] - '0', 110 minute = (s[3] - '0') * 10 + s[4] - '0', 111 second = (s[6] - '0') * 10 + s[7] - '0'; 112 return hour * 3600 + minute * 60 + second; 113 }