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:00

Sample Output:

1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <map>
 5 #include <string>
 6 #include <cstring>
 7 using namespace std;
 8 struct Node{
 9     int tim;
10     char state[5], carNo[8];
11 };
12 Node buf[10000], valid[10000];
13 int validNum = 0, N, K, maxTime = -1;
14 map<string, int> mp;
15 vector<string> ans;
16 
17 bool cmpByIdTime(const Node &a, const Node &b){
18     int rst = strcmp(a.carNo, b.carNo);
19     if(rst != 0)    return rst < 0;
20     return a.tim < b.tim;
21 }
22 
23 bool cmpByTime(const Node &a, const Node &b){
24     return a.tim < b.tim;
25 }
26 
27 void getValid(){
28     int i = 0;
29     while(i < N){
30         int in = -1;//每辆车要初始化为out ----- 在哪用, 在哪定义和初始化 ----这个bug花了好长时间 
31         string carNo(buf[i].carNo);
32         while(i < N && strcmp(carNo.c_str(), buf[i].carNo) == 0){
33             if(strcmp(buf[i].state, "in") == 0){
34                 in = i;
35             }else{//out
36                 if(in != -1){
37                     mp[carNo] += (buf[i].tim - buf[in].tim);
38                     valid[validNum ++] = buf[in];
39                     valid[validNum ++] = buf[i];
40                 }
41                 in = -1;
42             }
43             i ++;
44         }
45     }
46 }
47 
48 int main()
49 {
50     int hh, mm, ss, tim;
51     scanf("%d%d", &N, &K);
52     for(int i = 0; i < N; ++ i){ 
53         scanf("%s %02d:%02d:%02d %s", buf[i].carNo, &hh, &mm, &ss, buf[i].state);
54         buf[i].tim = hh*3600+mm*60+ss;
55     }
56     sort(buf, buf+N, cmpByIdTime);
57     getValid();//取得合法序对 
58     sort(valid, valid+validNum, cmpByTime);
59     int idx = 0, carNum = 0;
60     for(int i = 0; i < K; ++ i){
61         scanf("%02d:%02d:%02d", &hh, &mm, &ss);
62         tim = hh*3600+mm*60+ss;
63         while(idx < validNum && valid[idx].tim <= tim){
64             if(strcmp(valid[idx].state, "in") == 0)        carNum ++;
65             else carNum --;
66             idx ++;
67         }
68         printf("%d\n", carNum);
69     }
70     for(map<string, int> ::iterator it = mp.begin(); it != mp.end(); it ++){
71         if(it->second > maxTime){
72             maxTime = it->second;
73             ans.clear();    ans.push_back(it->first);
74         }else if(it->second == maxTime)    ans.push_back(it->first);
75     }
76     for(int i = 0; i < ans.size(); ++ i)    printf("%s ", ans[i].c_str());
77     printf("%02d:%02d:%02d\n", maxTime/3600, maxTime/60%60, maxTime%60);
78     return 0;
79 }

 

Posted on 2017-02-28 15:48  小小旅行商  阅读(164)  评论(0编辑  收藏  举报