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
浙江大学有8个校区和很多校门。我们可以收集到从每个校门进出的车辆信息和进出时间。现在这些信息都是可获得的,要求你得到任意时刻校园内的停车数,以及在每天中停车时间最久的车辆
昨天做了一整天,但一直提示超时,刚开始有3个测试点超时,最后剩下一个,但始终无法解决,今天就只能看答案了
首先构建结构体存储输入的车辆信息,其进出时间直接化成距离0时的秒数。输入完毕后,对该结构体按照车牌号的时间顺序进行排序,随后遍历,当出现一对有效信息时,则将这两个结构体存入Valid数组,并更新其累积停车时间(map存储)及最大累积停车时间。之后对Valid数组按时间进行排序。遍历查询某个时间点的停车数量(此处有技巧,不能用暴力法,详见代码)。遍历结束后,再遍历停车时间map,输出与最长累积停车时间相等的车牌号,最后按格式输出最长累积停车时间
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;
struct info {
char id[10];
int time;//距离当天零点秒数
int status;//0表示in,1表示out
}Cars[10005],Valid[10005];//分别代表所有信息和有效信息
struct ptrCmp {//定义char*比较规则
bool operator() (const char* s1, const char* s2) const {//必须有最后的const
return strcmp(s1, s2) < 0;
}
};
map<char*, int, ptrCmp> parkTIme;//存入车牌号及累积停车时间
bool cmp(info a, info b) {//按车牌号及时间排序
int flag = strcmp(a.id, b.id);
if (flag != 0) return flag < 0;
else if (a.time != b.time) return a.time < b.time;
else return a.status < b.status;
}
bool cmpTime(info a, info b) {//直接按时间排序
return a.time < b.time;
}
int main()
{
int N, K, i, h, m, s;
char tmp[4];
int ret = scanf("%d %d", &N, &K);
for (i = 0; i < N; ++i) {
ret = scanf("%s %d:%d:%d %s", Cars[i].id, &h, &m, &s, tmp);
Cars[i].time = h * 3600 + m * 60 + s;
Cars[i].status = (tmp[0] == 'i') ? 0 : 1;
}
sort(Cars, Cars + N, cmp);
int num = 0, numCar = 0, now = 0, maxTime = -1, time1;
for (i = 1; i < N; ++i) {
if (!strcmp(Cars[i - 1].id, Cars[i].id) && Cars[i - 1].status == 0 && Cars[i].status == 1) {
Valid[num++] = Cars[i - 1];//如果相邻两条信息有效,则加入Valid数组
Valid[num++] = Cars[i];
int inTime = Cars[i].time - Cars[i - 1].time;
parkTIme[Cars[i].id] += inTime;//累积停车时间
maxTime = max(maxTime, parkTIme[Cars[i].id]);//最大累积停车时间
}
else if (Cars[i].status == 1) ++i;
}
sort(Valid, Valid + num, cmpTime);
for (int k = 0; k < K; ++k) {
ret = scanf("%d:%d:%d", &h, &m, &s);
time1 = h * 3600 + m * 60 + s;
while (now < num && Valid[now].time <= time1) {//now指向记录的时间不超过查询时间
if (Valid[now].status == 0) ++numCar;//如果是进,停车数+1
else --numCar;//如果是出,停车数-1
++now;
}
printf("%d\n", numCar);
}
for (auto it = parkTIme.begin(); it != parkTIme.end(); ++it) {//找出累积最大时间对应的车牌号
if (maxTime == it->second) {
printf("%s ", it->first);
}
}
printf("%02d:%02d:%02d", maxTime / 3600, maxTime % 3600 / 60, maxTime % 60);
return 0;
}