1095. Cars on Campus (30)

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<string>
  2 #include<iostream>
  3 #include<map>
  4 #include<stdio.h>
  5 #include<vector>
  6 #include<algorithm>
  7 #include<string.h>
  8 #include<stdlib.h>
  9 using namespace std;
 10 
 11 struct car
 12 {
 13     bool tag;
 14     int t;
 15 };
 16 
 17 map<string, vector<car> > smap;
 18 
 19 int hOfcar[24];
 20 int numOfcar[24*60*60];
 21 
 22 bool cmp(car a,car b)
 23 {
 24     return a.t < b.t;
 25 }
 26 
 27 struct result
 28 {
 29     char name[10];
 30     int t;
 31     long long id;
 32 };
 33 
 34 long long toInt(char name[10])
 35 {
 36     long long id = 0;
 37     for(int i = 0;i <7;++i)
 38     {
 39         int tem = 0;
 40         if(name[i] >='0' && name[i] <= '9')
 41             tem = name[i] - '0';
 42         else if(name[i] >='A' && name[i] <= 'Z')
 43             tem = name[i] - 'A' + 10;
 44 
 45         id = id * 36 + tem;
 46     }
 47     return id;
 48 }
 49 
 50 bool cmp2(result a,result b)
 51 {
 52     return a.id < b.id;
 53 }
 54 int main()
 55 {
 56     int n,m,hh,mm,ss;
 57     scanf("%d%d",&n,&m);
 58     char name[10],tag[5];
 59     car ctem;
 60     for(int i = 0 ;i < n ;++i)
 61     {
 62         scanf("%s %d:%d:%d %s",name,&hh,&mm,&ss,tag);
 63         ctem.t = hh*60*60 + mm * 60 + ss;
 64         if(tag[0] == 'i')
 65         {
 66             ctem.tag = 0;
 67         }
 68         else ctem.tag = 1;
 69         smap[name].push_back(ctem);
 70     }
 71     vector<result> vv;
 72     for(map<string, vector<car> >::iterator it = smap.begin();it != smap.end();++it)
 73     {
 74         sort(it->second.begin(),it->second.end(),cmp);
 75         int size = it->second.size();
 76         int sum = 0;
 77         for(int i = 0 ;i < size -1 ;++i)
 78         {
 79             while(i < size -1 &&(!(!it->second[i].tag && it->second[i+1].tag)))
 80                 ++i;
 81             if(i < size -1)
 82             {
 83                 //不能直接遍历,否则最后一个case会超时
 84                 //hOfcar[k], 保存该车在第k时全都停留,则加一
 85                 int hlow = it->second[i].t / 3600 +1;
 86                 int hhigh = it->second[i+1].t / 3600;
 87                 for(int k = hlow ; k < hhigh;++k)
 88                     ++hOfcar[k];
 89                 //剩余部分 
 90                 if(hlow <= hhigh)
 91                 {
 92                     for(int k = it->second[i].t ;k < hlow * 3600 ;++k)
 93                         ++numOfcar[k];
 94                     for(int k = hhigh*3600 ;k < it->second[i+1].t ;++k)
 95                         ++numOfcar[k];
 96                 }
 97                 else//注意 in 和 out 是在同一个小时的情况!
 98                 {
 99                     for(int k = it->second[i].t ;k < it->second[i+1].t ;++k)
100                         ++numOfcar[k];
101                 }
102                 sum += it->second[i+1].t - it->second[i].t;
103             }
104         }
105         if(vv.empty() || sum == vv[0].t)
106         {
107             result rtem;
108             strcpy(rtem.name,it->first.c_str());
109             rtem.id = toInt(rtem.name);
110             rtem.t = sum;
111             vv.push_back(rtem);
112         }
113         else if(sum > vv[0].t)
114         {
115             vv.clear();
116             result rtem;
117             strcpy(rtem.name,it->first.c_str());
118             rtem.id = toInt(rtem.name);
119             rtem.t = sum;
120             vv.push_back(rtem);
121         }
122     }
123     for(int i = 0 ;i < m;++i)
124     {
125         scanf("%d:%d:%d",&hh,&mm,&ss);
126         int t = hh*60*60 + mm * 60 + ss;
127         printf("%d\n",hOfcar[hh]+numOfcar[t]);
128     }
129     sort(vv.begin(),vv.end(),cmp2);
130     for(int i = 0 ;i < vv.size();++i)
131     {
132         printf("%s ",vv[i].name);
133     }
134     printf("%02d:%02d:%02d\n",vv[0].t/3600,(vv[0].t%3600)/60,vv[0].t%60);
135     return 0;
136 }

 

posted @ 2016-02-23 18:54  小爷  阅读(628)  评论(0编辑  收藏  举报