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×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
题目大意:给出 n 个车牌号、时间点、进出状态的记录,然后查询 k 个时间点的校园内车辆数量。最后输出在校园里面停留时间最长的车的车牌号,以及停留时间。如果最长停留时间有多辆车,按照的车牌的字母序从小到大全部输出。注意记录的 配对要求,如果一个车多次进入未出,取最后一次进入;如果一个车多次出未进入,取第一次出。一辆 车可能多次出入校园,停车的总时间取所有进出的停留时间之和。
分析:为了简便计算,把每个时间点全部化为秒数,只有最后输出才转化为 小时:分钟:秒 来表示。读入所有记录后,先对记录排序,最终排序为车牌号相同,进出时间从早到晚。之后遍历所有的记录,找到所有能够匹配的,重新存储,此时只存储车牌号,和总停留时间。再读入查询。
对于每次查询,检查所有符合要求的记录,判断每两条 in 和 out 的记录的时间段,是否包括了查询的时间点。
最后将重新存储的总停留时间按照时长从大到小、车牌号从小到大排序,输出最大的停留时间和车牌号即可。
cpp
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
#define NUMBER_OF_THREADS 10
using namespace std;
typedef struct node
{
string plate,status;
int hh,mm,ss,times,sum;
}node;
bool cmp1(node a,node b)
{
if(a.plate!=b.plate)return a.plate<b.plate;
return a.times<b.times;
}
bool cmp2(node a,node b)
{
return a.times<b.times;
}
bool cmp3(node a,node b)
{
if(a.sum!=b.sum)return a.sum>b.sum;
return a.plate<b.plate;
}
int main(void)
{
#ifdef test
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
clock_t start=clock();
#endif //test
int n,k;scanf("%d%d",&n,&k);
node num[n+5],rec[n+5];
for(int i=0;i<n;++i)
{
cin>>num[i].plate;
scanf("%d:%d:%d",&num[i].hh,&num[i].mm,&num[i].ss);
cin>>num[i].status;
num[i].times=num[i].hh*60*60+num[i].mm*60+num[i].ss;
rec[i].sum=0;
}
int qh,qm,qs,qtimes;
int cnt[k+5]={0},ans[k+5]={0};
for(int i=0;i<k;++i)
{
scanf("%d:%d:%d",&qh,&qm,&qs);qtimes=qh*60*60+qm*60+qs;
cnt[i]=qtimes;
}
sort(num,num+n,cmp2);
sort(num,num+n,cmp1);
int t=0;
for(int i=1;i<n;)
{
if(num[i-1].plate==num[i].plate&&(num[i].status=="out"&&num[i-1].status=="in"))
{
if(!t||rec[t-1].plate!=num[i].plate)
{
rec[t].plate=num[i].plate,rec[t].sum+=num[i].times-num[i-1].times,t++;
}
else rec[t-1].sum+=num[i].times-num[i-1].times;
for(int j=0;j<k;++j)
if(cnt[j]<num[i].times&&cnt[j]>=num[i-1].times)ans[j]++;
i+=2;
}
else if(num[i].status=="out")i+=2;
else if(num[i].status=="in")i++;
}
for(int i=0;i<k;++i)
printf("%d\n",ans[i]);
sort(rec,rec+t,cmp3);
db1(t);
for(int i=0;i<t;++i)
db2(rec[i].plate,rec[i].sum);
int maxn=rec[0].sum;
for(int i=0;i<t;++i)
{
if(rec[i].sum==maxn)
{
cout<<rec[i].plate<<' ';
}
else break;
}
printf("%02d:%02d:%02d\n",maxn/3600,maxn%3600/60,maxn%60);
#ifdef test
clockid_t end=clock();
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n\n\n\n\n");
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
#endif //test
return 0;
}