您的位置:首页 > 其它

1095. Cars on Campus (30)

2018-01-06 23:21 218 查看

1095. Cars on Campus (30)

时间限制220 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueZhejiang 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 numberof 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 formatplate_number hh:mm:ss statuswhere 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 iseither 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 notpaired 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
--------------------------
注意点:此题若忽略上面加粗标红的note,最后一个点容易超时。
核心代码,即查询代码,已着重注释。利用好 查询的时间是递增 这一条件,可以使用顺序查找而不用两层for循环,降了一档复杂度。
#include <cstdio>#include <cstdlib>#include <iostream>#include <algorithm>#include <string>#include <cstring>#include <vector>#include <map>using namespace std;struct node{char id[20];int hh,mm,ss;bool flag;//0表示in 1表示out}T[10005],valid[10005];//valid数组是筛选掉无用数据之后的记录int validnum=0;//有效记录的记录数量bool cmp(node a,node b){if(strcmp(a.id,b.id)!=0)return strcmp(a.id,b.id) < 0;else if(a.hh!=b.hh)return a.hh < b.hh;else if(a.mm!=b.mm)return a.mm < b.mm;elsereturn a.ss < b.ss;}bool cmp2(node a,node b){if(a.hh!=b.hh)return a.hh < b.hh;else if(a.mm!=b.mm)return a.mm < b.mm;elsereturn a.ss < b.ss;}int fun(node a)//返回时间对应的秒数{return a.hh*60*60+a.mm*60+a.ss;}map<string,int> mp;int main(){//  freopen("in.txt","r",stdin);int N,K,maxtime=0;scanf("%d %d",&N,&K);for(int i=0;i<N;i++){char tstr[20];scanf("%s %d:%d:%d %s",T[i].id,&T[i].hh,&T[i].mm,&T[i].ss,tstr);if(strcmp(tstr,"in")==0)T[i].flag=0;elseT[i].flag=1;}sort(T,T+N,cmp);int time=0;for(int i=0;i<N-1;i++){if( strcmp(T[i].id,T[i+1].id)==0 && T[i].flag==0 && T[i+1].flag==1)//匹配成功{valid[validnum++]=T[i];valid[validnum++]=T[i+1];time+=fun(T[i+1])-fun(T[i]);}else if(strcmp(T[i].id,T[i+1].id)!=0)//换车{mp[T[i].id]=time;//记录,存进数组if(time>maxtime)maxtime=time;time=0;}}if(time!=0)//补上最后一条{mp[T[N-1].id]=time;if(time>maxtime)maxtime=time;}sort(valid,valid+validnum,cmp2);//使valid按时间排序int p=0;int cou=0;for(int i=0;i<K;i++){node tnode;scanf("%d:%d:%d",&tnode.hh,&tnode.mm,&tnode.ss);while(p<validnum && fun(valid[p])<=fun(tnode) )//!!关键代码:将所有有效记录从早到晚浏览一遍,边浏览边记录车的出入情况,每到一个查询时间就输出当时的车数。注意,如果查询的时刻正好有车辆out,那么那辆车不算在学校内部{if(valid[p].flag==0)cou++;elsecou--;p++;}printf("%d\n",cou);}map<string,int>::iterator it;for(it=mp.begin();it!=mp.end();it++){if(it->second==maxtime)printf("%s ",it->first.c_str());//!!c_str():string->%s,比直接cout要快}printf("%02d:%02d:%02d",maxtime/3600,maxtime%3600/60,maxtime%60);return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: