1095. Cars on Campus

来源:互联网 发布:素描画图软件 编辑:程序博客网 时间:2024/05/16 08:58

1095. Cars on Campus (30)

时间限制
220 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 7JH007BD 18:00:01 inZD00001 11:30:08 outDB8888A 13:00:00 outZA3Q625 23:59:50 outZA133CH 10:23:00 inZD00001 04:09:59 inJH007BD 05:09:59 inZA3Q625 11:42:01 outJH007BD 05:10:33 inZA3Q625 06:30:50 inJH007BD 12:23:42 outZA3Q625 23:55:00 inJH007BD 12:24:23 outZA133CH 17:11:22 outJH007BD 18:07:01 outDB8888A 06:30:50 in05:10:0006:30:5011:00:0012:23:4214:00:0018:00:0023:59:00
Sample Output:
1452101JH007BD ZD00001 07:20:09
这个题好繁琐啊,看着就感觉很无力,只好模拟了。用数组存放某一时刻出入车的数量,查询的时候按时序来依次统计,可以累计。
处理信息的时候可以先同车按照时序来,根据in和out选出信息对。
输入信息一次,排序一次,再遍历一次信息,再根据查询时间统计车数量。

#include<iostream>#include<algorithm>#include<string>#include<vector>#include<set>using namespace std;typedef struct node{string id;int t;bool state;};bool cmp(node x,node y){if(x.id!=y.id)return x.id<y.id;elsereturn x.t<y.t;}node info[10000];vector <int> incnt(24*60*60);vector <int> outcnt(24*60*60);int main(){int n,k;cin>>n>>k;for(int i=0;i<n;++i){int h,m,s;string state;cin>>info[i].id;scanf("%d:%d:%d",&h,&m,&s);cin>>state;info[i].t=h*3600+m*60+s;info[i].state=(state=="in")?true:false;}sort(info,info+n,cmp);set <string> maxid;int maxt=0;int sumt=0;for(int i=1;i<n;i++){if(info[i].id==info[i-1].id){if(!info[i].state&&info[i-1].state){incnt[info[i-1].t]++;outcnt[info[i].t]++;sumt=sumt+info[i].t-info[i-1].t;if(sumt>maxt){maxt=sumt;maxid.clear();maxid.insert(info[i].id);}else if(sumt==maxt)maxid.insert(info[i].id);}}else{sumt=0;}}int carnum=0;int cnt=0;for(int i=0;i<k;++i){int h,m,s;scanf("%d:%d:%d",&h,&m,&s);int t=h*3600+m*60+s;while(cnt<=t){carnum=carnum+incnt[cnt]-outcnt[cnt];cnt++;}cout<<carnum<<endl;}for(auto str=maxid.begin();str!=maxid.end();++str)cout<<*str<<" ";printf("%02d:%02d:%02d\n",maxt/3600,maxt%3600/60,maxt%3600%60);return 0;}


0 0