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
按车牌升序,时间升序对结构体进行排列。车牌和in、out都匹配时,累加该车牌的总停留时间,并与已知的最长时间比较,判断是否更新最长时间和车牌。用两个数组分别记录某一时刻in的车数和out的车数,使用一个变量记录校园当前车数,对这两个数组进行一次同步的遍历可以得到所有时刻校园内的车数。
/*2015.7.31cyq*/ #include <iostream> #include <fstream> #include <algorithm> #include <vector> #include <string> using namespace std; //ifstream fin("case1.txt"); //#define cin fin struct node{ string plate; int time; bool flag; //false表示in }; bool cmp(const node &a,const node &b){//车牌升序,时间升序 if(a.plate<b.plate) return true; else if(a.plate==b.plate){ if(a.time<b.time) return true; } return false; } int main(){ int N,K; scanf("%d%d",&N,&K); vector<node> data(N); string stime; char a[9]; int h,m,s; for(int i=0;i<N;i++){ scanf("%s",a); data[i].plate=a; scanf("%d:%d:%d",&h,&m,&s); data[i].time=(h*60+m)*60+s; scanf("%s",a); if(a[0]=='i') data[i].flag=false;//in else data[i].flag=true;//out } sort(data.begin(),data.end(),cmp); vector<int> inTime(24*60*60,0);//该时刻in的车数 vector<int> outTime(24*60*60,0);//该时刻out的车数 string curPlate=data[0].plate;//当前车牌 int curTime=0;//当前车牌累计的总时间 vector<string> maxPlate; //停最久的车 int maxTime=0; //最长时间 for(int i=1;i<N;i++){ if(data[i].plate==data[i-1].plate){//车牌匹配 if(data[i].flag&&!data[i-1].flag){//in out匹配 curTime+=data[i].time-data[i-1].time; if(curTime>maxTime){ maxTime=curTime; maxPlate.clear(); maxPlate.push_back(curPlate); }else if(curTime==maxTime){ maxPlate.push_back(curPlate); } inTime[data[i-1].time]++;//该时刻in的车数 outTime[data[i].time]++;//该时刻out的车数 } }else{//新车牌 curPlate=data[i].plate; curTime=0; } } vector<int> result(24*60*60,0); //result[i]表示i时刻的车数 int TT=0;//校园内当前车数 for(int i=0;i<24*60*60;i++){ TT=TT+inTime[i]-outTime[i]; result[i]=TT;//i时刻的车数 } while(K--){ scanf("%d:%d:%d",&h,&m,&s); printf("%d\n",result[(h*60+m)*60+s]); } for(auto it=maxPlate.begin();it!=maxPlate.end();++it) cout<<*it<<" "; printf("%02d:%02d:%02d",maxTime/3600,maxTime%3600/60,maxTime%60); return 0; }