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 (≤10^4 ), the number of records, and K (≤8×10^4 ) 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.考虑如何存储记录:显然,这道题很容易想到用结构体数组来存储记录。但容易忽略的是时间的存储,上面已经说过,以秒为单位来存储时间能够大大节约数据处理的时间,所以,结构体如下:
const int maxn=10010;
struct Car
{
char id[10];
char status[4];
int times;
}all[maxn],valid[maxn];
这里用all数组存储原始的输入数据,而valid数组则用于存储有效记录。考虑到原始记录不超过10^4条,所以这里数组大小设置为10010(多定义10个单元避免越界)。另外,我们还需要将输出的时间换算为以s为单位,可以专门定义一个函数来实现时间的换算:
int timeToInt(int h,int m,int s)
{
return h*3600+m*60+s;
}
2.考虑如何存储各车辆的停车总时间:定义map容器建立车牌号与停车时间之间的映射关系,能够大大增加处理效率。
3.存储好原始记录后,需要按最容易处理的方式对记录进行排序。由于我们需要首先筛选出有效记录,所以,将同一辆车的进出记录按时间先后放在一起是最佳的排序方式。所以定义以下排序规则:
bool cmp1(Car a,Car b)
{
if(strcmp(a.id,b.id)
return strcmp(a.id,b.id)<0;
else
return a.times
筛选有效记录的程序流程为:如果第i个记录与第i+1个记录车牌号相同,且status分别为“in”和“out”,说明这两记录均为有效记录,存入valid数组,否则不予理会。另外,在得到这两条有效记录的同时,还可以得到该车本次停车时间inTime,并将其累加到map容器parkTime中相应的映射中,同时,用maxTime筛选出最长停车时间。这样,当all数组枚举完成时,parkTime容器就能够完善好,并能筛选出停车时间最长的车辆。
4.考虑到还需要输出某一时刻校园内的车辆数,容易想到对有效记录valid数组按时间顺序进行排序并枚举valid数组,设置一个变量carNum=0用于记录当前校园内的车辆总数,当记录中的状态为“in”时,carNum++,当枚举到的记录状态为“out”时,carNum–。所以我们还需要定义一个排序规则对有效记录按时间先后进行排序:
bool cmp2(Car a,Car b)
{
return a.times
5.输出停车时间最长的车辆车牌号可使用语句:
map::iterator it//这是定义一个map的迭代器
for(it=parkTime.begin();it!=parkTime.end();it++)//遍历map容器的方法
{
if(it->second==maxTime)
printf("%s ",it->first.c_str());//注意掌握map容器的使用方法
}
输出最长停车时间可在输出时对maxTime进行换算:
printf("%02d:%02d:%02d",maxTime/3600,maxTime%3600/60,maxTime%3600%60);
注意:
1.parkTime的初始化:
if(parkTime.count(all[i].id)==0)//如果map容器中还没有关键字all[i].id对应的记录
{
parkTime[all[i].id]=0;//在map中新增该条记录,并将对应的值设为0
}
2.注意时间的输出格式:时,分,秒都要输出为两位,不足两位的在前面补0
完整代码如下:
#include
#include
#include
#include
以上代码已通过测试
Atalanta