目录
1,题目描述
- the plate numbers of the cars crossing the gate:过门车辆的车牌号;
- chronologically:按时间顺序;
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
题目大意
记录每辆车进出校园的车牌号(plate)、出入时间,要求:对一系列查询(具体的时间点,且按照增序给出),输出这个时间点,校园内的车辆数目。在最后输出校园内停留时间最长的辆车(1.可能是多辆,2.一辆车可能进出多次),及停留时间。
注意
- Any
in
records that are not paired with anout
record are ignored, as areout
records not paired with anin
record:忽略没有配对的记录; - output all of their plate numbers in a line in alphabetical order:停留时间最长的辆车可能不唯一;
2,思路
参考大神@日沉云起【pat甲级-1095-Cars on Campus (30)】
数据结构
- struct record{
string name;
int time;
bool in; //true为进 false为出
record(string s, int t, bool i) : name(s), time(t), in(i) {}//构造函数
};每条记录的形式; - unordered_map<string, vector<record> > allCar:所有记录(包括无效的记录)。每个车辆对应的所有进出时间;(unordered无序map,速度比mao快。虽然自己测试的时候仍然是排序了的(・∀・(・∀・(・∀・*))
- vector<record> validRec:筛选出所有的有效记录(in与out交替且配对),每个记录作为一个元素,对这些元素排序,遍历的时候即可方便地获取校园内车辆的数目;
- set<string> maxPlate:等待时长最长的车牌号。可以想象成能自动排序的容器(set是自动按照字母顺序对元素进行排序);
算法
- 按照车牌号接受所有记录,存入allData:
- 对每辆车进行排序,并筛选出in与!in交替的有效记录,存入validRec。同时计算每辆车的等待时间,求出等待时长最大的所有车辆放入maxPlate(记得每次找到更大值时,调用clear函数),以及最长等待时间maxTime:
- 对所有有效记录按照时间先后顺序排序。遍历有效记录,in加一,!in减一,并且由于查询是按增序的,故记录index可避免重复计算:
- 输出停留最久的车辆的车牌号(使用printf输出string类型时,需要使用c_str()函数哦),及时长:
3,AC代码
仿照大神的代码(tql,逻辑清晰且简洁d=====( ̄▽ ̄*)b)
#include<bits/stdc++.h>
using namespace std;
struct record{
string name;
int time;
bool in; //true为进 false为出
record(string s, int t, bool i) : name(s), time(t), in(i) {}
};
bool cmp1(record a, record b){
return a.time < b.time;
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, K; //N记录数目 K查询数目
scanf("%d %d", &N, &K);
unordered_map<string, vector<record> > allCar;
string s1, s2;
s1.resize(7); //设置长度 这样可以使用scanf 速度更快
int h, m, s;
for(int i = 0; i < N; i++){
scanf("%s %d:%d:%d", &s1[0], &h, &m, &s);
cin>>s2; //s2不定长
allCar[s1].push_back(record(s1, h * 3600 + m * 60 + s, (s2 == "in"))); //妙啊
}
vector<record> validRec; //有效数据
set<string> maxPlate; //等待时长最长的车牌
int maxTime = 0;
for(auto it : allCar){
sort(it.second.begin(), it.second.end(), cmp1);
int t = 0; //记录当前车辆的停车时长
for(int i = 0; i < it.second.size() - 1; i++){ //筛选有效记录(先in后!in)注意边界
if(it.second[i].in && !it.second[i+1].in){
validRec.push_back(it.second[i]);
validRec.push_back(it.second[i+1]);
t += (it.second[i+1].time - it.second[i].time);
}
}
if(t > maxTime){
maxTime = t;
maxPlate.clear();
maxPlate.insert(it.first);
}else if(t == maxTime)
maxPlate.insert(it.first); //时长相同且最长放入map中(自动按字典排序)
}
sort(validRec.begin(), validRec.end(), cmp1);
int num = 0, index = 0, t; //num校园内车辆数目 index当前遍历记录的下标(查询是按增序 故可累计)
while(K--){
scanf("%d:%d:%d", &h, &m, &s);
t = h * 3600 + m * 60 + s;
for(; index < validRec.size() && validRec[index].time <= t; index++){
num += validRec[index].in == true ? 1 : -1; //妙啊
}
printf("%d\n", num);
}
for(auto it : maxPlate)
printf("%s ", it.c_str());
printf("%02d:%02d:%02d", maxTime / 3600, maxTime % 3600 / 60, maxTime % 60);
return 0;
}
4,解题过程
第一搏
(没有想到合适的数据结构ε(┬┬﹏┬┬)3,直接请大神了)
不把一辆车的出和入时间放入一个结构体!只记录一个时间(入/出),bool型变量in判断是入还是出。这样,对记录排序后,即可根据时间先后以及进/出来计算校园内车辆数目。