【PAT】【Advanced Level】1095. Cars on Campus (30)
来源:互联网 发布:js实现点击重置按钮 编辑:程序博客网 时间:2024/05/29 13:13
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 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:00Sample Output:
1452101JH007BD ZD00001 07:20:09
https://www.patest.cn/contests/pat-a-practise/1095
思路:
做法不难,但是比较麻烦,而且容易超时
首先所有记录按时间排序
然后进行匹配,遇到in,用map映射in的时间
遇到out,从map中找in,找到的话,将间隔加入另一个map。
同时设立in、out数组存储所有进、出的时间
对于求最大间隔的车,按时间扫描的过程中,实时维护最大值即可
对于求实时车辆数目
由于给定的是有序时间,所以,现将in、out排序,然后对于每一个查询时间,in中每有一辆在其之前进入的,则数目加一,反之,out中每有一辆在其之前出,数目减一。后一步可以在前一步的结果之上继续计算,降低复杂度。
CODE:
#include<iostream>#include<map>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>#define N 100010using namespace std;typedef struct S{string name;int ti;string bj;};vector<S> rec;map<string,int> ma;map<string,int> ma1;vector<string> ar;int maxn;vector<int> _in;vector<int> _out;bool cmp(S a,S b){return a.ti<b.ti;}bool cmp1(string a, string b){return a<b;}bool cmp2(int a,int b){return a<b;}int main(){int n,m;scanf("%d %d",&n,&m);maxn=-1;for (int i=0;i<n;i++){S ne;char aa[10],bb[5];string a,b;int ho,mi,se;scanf("%s",aa);//cin>>a;scanf("%d:%d:%d",&ho,&mi,&se);scanf("%s",bb);//cin>>b;ne.name=aa;ne.bj=bb;ne.ti=3600*ho+60*mi+se;;rec.push_back(ne);//cout<<rec[i].name<<" "<<rec[i].bj<<" "<<rec[i].ho<<" "<<rec[i].mi<<" "<<rec[i].se<<endl;}sort(rec.begin(),rec.end(),cmp);for (int i=0;i<n;i++){string a=rec[i].name;string bb=rec[i].bj;int ti=rec[i].ti;if (bb=="in"){ma[a]=ti+1;}else if (bb=="out"){if (ma[a]!=0){int intr=ti-ma[a]+1;_in.push_back(ma[a]-1);_out.push_back(ti);ma1[a]+=intr;if (ma1[a]>=maxn){if (ma1[a]>maxn) ar.clear();maxn=ma1[a];ar.push_back(a);}ma[a]=0;}}}sort(ar.begin(),ar.end(),cmp1);sort(_in.begin(),_in.end(),cmp2);sort(_out.begin(),_out.end(),cmp2);int re=0;int n1=0;int n2=0;for (int i=0;i<m;i++){int ho,mi,se;scanf("%d:%d:%d",&ho,&mi,&se);int fi=ho*3600+mi*60+se;while (n1<_in.size()&&fi>=_in[n1]){re++;n1++;}while (n2<_out.size()&&fi>=_out[n2]){re--;n2++;}printf("%d\n",re);}for (int i=0;i<ar.size();i++){if (i!=0) printf(" ");printf("%s",ar[i].c_str());}int intr=maxn;printf(" %02d:%02d:%02d",intr/3600,intr%3600/60,intr%60);return 0;}
- 【PAT】【Advanced Level】1095. Cars on Campus (30)
- PAT (Advanced Level) Practise 1095Cars on Campus (30)
- PAT (Advanced Level) Practise 1095 Cars on Campus (30)
- PAT (Advanced Level) 1095. Cars on Campus (30) 结构体排序,合并区间
- 1095. Cars on Campus (30)——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1095(Cars on Campus)
- PAT A 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- 【PAT】1095. Cars on Campus (30)
- pat-A 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- 1095. Cars on Campus (30) PAT 甲级
- PAT 1095. Cars on Campus (30)
- PAT-A-1095. Cars on Campus (30)
- PAT A 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- Pat(A) 1095. Cars on Campus (30)
- Android高效加载大图、多图解决方案,有效避免程序OOM
- Spring之IOC、AOP的理解
- JQ实现轮播
- docker centos redis 基础常用命令
- 【PAT 1004 Acute Stroke (30)】& dfs
- 【PAT】【Advanced Level】1095. Cars on Campus (30)
- BeautifulSoup库中find_all()方法
- 2017最新java学习大全
- Silver Cow Party
- Retrofit和Rxjava的简单了解
- tf-idf:sklearn中TfidfVectorizer使用
- spring-task
- 【Java】split(".")
- Attack on Titans ZOJ