Zhejiang University has 6 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 (<= 10000), the number of records, and K (<= 80000) 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:00Sample Output:
1 4 5 2 1 0 1 JH007BD ZD00001 07:20:09
//1095
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
struct record
{
int time;//the time of this record
int in;//the status of the plate
record(int t,int i):time(t),in(i){}
bool operator <(const record &r)const
{
return time<r.time;
}
};
int name2id(string str)
{//transform the plate's name to a id number
int num=0;
for(int i=0;i<7;++i)
{
if(str[i]>='0'&&str[i]<='9')
num = num*36+str[i]-'0';
else
num = num*36+str[i]-'A'+10;
}
return num;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
map<int,int>id2idx;//the id of plate to its index
vector<string>platename;//the name of each plate
vector<vector<record>>all;//all record
for(int i=0;i<n;++i)
{
string name;
char status[5];
int hour,min,sec;
cin>>name;
scanf("%d:%d:%d %s",&hour,&min,&sec,status);
record r(hour*3600+min*60+sec,status[0]=='i'?1:0);
int id=name2id(name);
if(id2idx.count(id))
all[id2idx[id]].push_back(r);
else
{//insert it of it is a new plate
id2idx.insert(pair<int,int>(id,all.size()));
platename.push_back(name);
vector<record>rec;
rec.push_back(r);
all.push_back(rec);
}
}
for(int i=0;i<all.size();++i)
sort(all[i].begin(),all[i].end());
vector<int>cur;//current index
cur.assign(all.size(),0);
vector<int>period;
period.assign(all.size(),0);
for(int i=0;i<k;++i)
{
int hour,min,sec;
scanf("%d:%d:%d",&hour,&min,&sec);
int t=hour*3600+min*60+sec;
int count=0;//the number of cars on plate at this time
for(int j=0;j<all.size();++j)
{
while(1)
{
//if this time is earlier than the earlist record of this plate
//or later than the last record of this plate
if((cur[j]==0&&t<all[j][0].time) || cur[j]>=all[j].size()-1)
break;
if(t>=all[j][cur[j]].time && t<all[j][cur[j]+1].time)
{//if this time is just between the current record and next recod
if(all[j][cur[j]].in && !all[j][cur[j]+1].in)
++count;
break;
}
//calculate the period of each plate
if(all[j][cur[j]].in && !all[j][cur[j]+1].in)
period[j] += all[j][cur[j]+1].time-all[j][cur[j]].time;
++cur[j];
}
}
printf("%d\n",count);
}
int maxperiod=0;
for(int i=0;i<all.size();++i)
{
while(cur[i]<all[i].size()-1)
{//calculate the period of the rest recods
if(all[i][cur[i]].in && !all[i][cur[i]+1].in)
period[i] += all[i][cur[i]+1].time-all[i][cur[i]].time;
++cur[i];
}
if(period[i]>maxperiod)
maxperiod=period[i];
}
for(int i=0;i<all.size();++i)
if(maxperiod==period[i])
cout<<platename[i]<<' ';
printf("%02d:%02d:%02d",maxperiod/3600,(maxperiod%3600)/60,maxperiod%60);
return 0;
}