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 (≤104), the number of records, and K (≤8×104) 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
题目要求
给出n个车牌号、时间点、进出状态记录,查询k个时间点对应的校园内车辆数,最后输出在校园里呆的时间最长的车的车牌号,多个按字母从小到大输出车牌,并输出呆的时间。
一次合理的进出记录配对应该是同一个车牌时间相邻的in和out
解题思路
这里将h:m:s转化为相距00:00:00的秒数time=h*3600+m*60+s
对所有记录按车牌从小到大排序,相同车牌按时间先后进行排序,找到其中合理的记录。
在找合理记录的同时进行最长停留时间判断,由于已经按照车牌从小到大排序,所以对同一辆车的多条合理记录是连续的,利用一个暂存车牌temp,这样当第一次出现该车牌的记录时初始化停留时间t,同时重置暂存车牌temp,如果下一条记录还为该车,则继续加上该次停留的时间。每次都要比较该车当前停留时间t与最大停留时间的大小,然后利用vector保存对应的下标
将所有合理的记录按照时间先后进行排序。因为查询是按时间升序排序的,用一个从0 到3600*24-1的循环来模拟从00:00:00到23:59:59的每个时间,用pnum[i]代表第is对应的停留在校园中车的数量。即pnum[i]=pnum[i-1]+x,其中x是指可变量
若当前时间是否对应有对应的进出记录,in则x=1,out则x=-1
这样可以提前计算完所有时间对应的校园内停车数量。
对应给定的查询时间,只需要转化为秒然后根据这个时间作为下标time所得pnum[time]的值即可
注意
1.这里的呆的时间最长并非指单次呆的时间最长,而是在校园里一共呆的时间最长。因为一个车可能进出校园多次
完整代码
#include<bits/stdc++.h>
using namespace std;
struct Rec{
char pno[9];
int h,m,s,time,status;
};
Rec rec[10010];
bool com1(Rec r1,Rec r2){
if(strcmp(r1.pno,r2.pno)<0) return true;
else if(strcmp(r1.pno,r2.pno) == 0){
return r1.time < r2.time;
}else return false;
}
bool com2(Rec r1,Rec r2){
return r1.time < r2.time;
}
int main(){
int n,k,i,j,end,t=0,tmax=0,p=0;
char temp[10];
int pnum[90000],parttime[10010];
memset(pnum,0,sizeof(pnum));
memset(parttime,0,sizeof(parttime));
scanf("%d %d\n",&n,&k);
vector<Rec> v;
for(i=0;i<n;i++){
scanf("%s %d:%d:%d %s",rec[i].pno,&rec[i].h,&rec[i].m,&rec[i].s,temp);
rec[i].status = (strcmp(temp,"in") == 0)? 0:1;
rec[i].time = rec[i].h*3600 + rec[i].m*60+rec[i].s;
}
sort(rec,rec+n,com1);
vector<int> vmax;
for(i=1;i<n;i++){
if(strcmp(rec[i].pno,rec[i-1].pno) == 0 && rec[i-1].status == 0 && rec[i].status == 1){
v.push_back(rec[i-1]);
v.push_back(rec[i]);
if(strcmp(temp,rec[i].pno) != 0){
t=0;
strcpy(temp,rec[i].pno);
}
t+=rec[i].time - rec[i-1].time;
if(t>tmax){
vmax.clear();
vmax.push_back(i);
tmax=t;
}else if(t==tmax) vmax.push_back(i);
}
}
sort(v.begin(),v.end(),com2);
end=3600*24;
for(i=0;i<end;i++){
if(i!=0) pnum[i]=pnum[i-1];
if(v[p].time == i){
if(v[p].status == 0) pnum[i]+=1;
else if(v[p].status == 1) pnum[i]-=1;
p++;
while(v[p].time == i){
if(v[p].status == 0) pnum[i]+=1;
else if(v[p].status == 1) pnum[i]-=1;
p++;
}
}
}
int h,m,s,time;
for(i=0;i<k;i++){
scanf("%d:%d:%d",&h,&m,&s);
time=h*3600+m*60+s;
printf("%d\n",pnum[time]);
}
for(i=0;i<vmax.size();i++){
printf("%s ",rec[vmax[i]].pno);
}
printf("%02d:%02d:%02d\n",tmax/3600,tmax%3600/60,tmax%60);
return 0;
}