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; andstatus 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
题目大意:
给n个车辆进出车库的记录信息,k个询问,每个询问输出当前时间车库中有多少辆车;
最后一行输出在车库停留时间最长的车(如果有多辆,按字典序排列),并输出停留的时间。
题目解析
模拟题,具体思路代码注释中很清楚
题中给的便利条件
1、询问按时间顺序询问
2、对于同一辆车不会在同一个时间点进入和“out”
坑点:
1、对于一辆车:如果该辆车按时间顺序的记录如下:in -> in -> in -> out -> out-> in -> in,那么只有红色字体的记录是有效的
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 1e4 + 10;
const int MAXK = 8e4 + 10;
struct NODE{
string name;///string方便排序和用stl
int time;///进入或出去时间
bool status;///1:进入 0:出去
}node[MAXN];
int Cal_time(int x, int y, int z){
return (x*3600+y*60+z);
}
bool cmp1(NODE a, NODE b){return a.time<b.time;}///时间排序
bool cmp2(NODE a, NODE b){
if(a.name==b.name) return a.time<b.time;
return a.name<b.name;
}///名字排序 时间小的在前面
bool cmp3(string a, string b){return a<b;}
int main(){
int n, k;
while(~scanf("%d%d", &n, &k)){
int hh, mm, ss;
for(int i=0; i<n; i++){
char st[5];
cin>>node[i].name;
scanf("%d:%d:%d%s", &hh, &mm, &ss, st);
node[i].time=Cal_time(hh, mm, ss);
if(strcmp(st, "in")==0) node[i].status=true;///true为进入
else node[i].status=false;
}
sort(node, node+n, cmp2);///名字排序(时间小的在前面)
int cnt=0;
for(int i=0; i<n-1; i++){///排除不合法后的记录数
if(node[i].name==node[i+1].name && node[i].status && !node[i+1].status){
node[cnt++]=node[i];
node[cnt++]=node[i+1];
}
}
sort(node, node+cnt, cmp1);///按时间排序
int pos=0;///标记询问过程中的断点
set<string>set_num;
map<string, int>pre_time, cal_tim;
while(k--){
scanf("%d:%d:%d", &hh, &mm, &ss);
int ti=Cal_time(hh, mm, ss);
for(int i=pos; i<cnt; i++){
string now=node[i].name;
if(node[i].time>ti){pos=i;break;}///该条记录的时间超过当前询问的时间
if(node[i].status){///进入
set_num.insert(now);
pre_time[now]=node[i].time;///记录该车牌号进入的时间
}else{
cal_tim[now]+=(node[i].time-pre_time[now]);
set_num.erase(now);
}
if(i==(cnt-1)) pos=cnt;
}
printf("%d\n", set_num.size());
}
for(int i=pos; i<cnt; i++){///询问结束,但是车辆的进入信息还没有计算完****(这点容易被忽略)
string now=node[i].name;
if(node[i].status) pre_time[now]=node[i].time;
else cal_tim[now]+=(node[i].time-pre_time[now]);
}
int max_time=-1;
vector<string>output;
for(map<string, int>::iterator it=cal_tim.begin(); it!=cal_tim.end(); it++){///遍历cal_tim求最大值
string now=it->first;
int now_ti=it->second;
if(now_ti > max_time){
output.clear();
max_time=now_ti;
output.push_back(now);
}else if(now_ti == max_time) output.push_back(now);
}
sort(output.begin(), output.end(), cmp3);
for(int i=0; i<output.size(); i++) cout<<output[i]<<' ';
printf("%02d:%02d:%02d\n", max_time/3600, (max_time%3600)/60, max_time%60);
}
return 0;
}