首页 > 其他 > 详细

A1095 Cars on Campus (30 分)

时间:2019-06-27 22:56:21      阅读:162      评论:0      收藏:0      [点我收藏+]

Zhejiang University has 8 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 (≤), the number of records, and K (≤) 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 accendingorder 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.查询时 now 必须放在外面不然会重复循环导致超时!!!
    2.清空cout缓存用cout<<flush;否则使用ios::sync_with_stdio(false);
cin.tie(0)时不与printf兼容,可能输出顺序错误。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 
  4 const int maxn=10010;
  5 
  6 struct Record{
  7     string carId;
  8     int hh;
  9     int mm;
 10     int ss;
 11     int time;
 12     string status;
 13     int flag;
 14 }rec[maxn],valid[maxn];
 15 
 16 int n,k;
 17 
 18 map<string,int> parkTime;
 19 
 20 int num=0;
 21 int maxTime=-1;
 22 
 23 
 24 bool cmp1(Record a,Record b){
 25     if(a.carId!=b.carId)
 26         return a.carId<b.carId;
 27     else
 28         return a.time<b.time;
 29 }
 30 
 31 bool cmp2(Record a,Record b){
 32     
 33     return a.time<b.time;
 34     
 35 }
 36 
 37 
 38 
 39 int main(){
 40     
 41     ios::sync_with_stdio(false);
 42     cin.tie(0);
 43     
 44     cin>>n>>k;
 45     
 46 //    scanf("%d%d",&n,&k);
 47     
 48     
 49     
 50     char c;
 51     Record temp;
 52     
 53     for(int i=0;i<n;i++){
 54         cin>>temp.carId>>temp.hh>>c>>temp.mm>>c>>temp.ss>>temp.status;
 55         
 56         //char str1[100];
 57         //char str2[100];
 58         //scanf("%s %d:%d:%d %s",&str1[0],&temp.hh,&temp.mm,&temp.ss,&str2[0]);
 59         //temp.carId=str1;
 60         //temp.status=str2;
 61         
 62             
 63         temp.time=temp.hh*3600+temp.mm*60+temp.ss;
 64         
 65         if(temp.status=="in")
 66             temp.flag=1;
 67         else
 68             temp.flag=2;
 69             
 70         rec[i]=temp;
 71     }    
 72     
 73     
 74     sort(rec,rec+n,cmp1);
 75     
 76     
 77     for(int i=0;i<n-1;i++){
 78         if(rec[i].carId==rec[i+1].carId&&
 79         rec[i].flag==1&&rec[i+1].flag==2){
 80             valid[num++]=rec[i];
 81             valid[num++]=rec[i+1];
 82             
 83             string carId=rec[i].carId;
 84             
 85             if(parkTime.count(carId)==0){
 86                 parkTime[carId]=0;
 87             }
 88             
 89             parkTime[carId]+=(rec[i+1].time-rec[i].time);
 90             
 91             maxTime=max(maxTime,parkTime[carId]);
 92             
 93         }
 94     }
 95     
 96     
 97     
 98     sort(valid,valid+num,cmp2);
 99     
100     int now=0,nowCar=0;  //now必须放外面
101     
102     for(int i=0;i<k;i++){
103         int hh,mm,ss;
104         char c;
105         
106         cin>>hh>>c>>mm>>c>>ss;
107         
108         //scanf("%d:%d:%d",&hh,&mm,&ss);
109         
110         int queryTime=hh*3600+mm*60+ss;
111         
112     
113         
114         
115         while(now<num&&valid[now].time<=queryTime){
116             if(valid[now].flag==1) nowCar++;
117             else nowCar--;
118             
119             now++;
120             
121         }
122         
123         cout<<nowCar<<endl;    
124         
125         //printf("%d\n",nowCar);    
126         
127     }
128     
129     
130 
131     
132     for(map<string,int>::iterator it = parkTime.begin(); it != parkTime.end();it++){
133             if(it->second==maxTime)
134             {
135                 cout<<it->first<<" ";
136                 
137             //    printf("%s ",it->first.c_str());
138             }
139     }
140     
141     cout<<flush;  //清空cout缓存
142     
143     //cout<<maxTime<<" "<<parkTime["JH007BD"]<<" "<<parkTime["ZD00001"]<<endl;
144     
145     printf("%02d:%02d:%02d\n",maxTime/3600,(maxTime/60)%60,maxTime%60);
146     
147     
148     
149     return 0;
150 }

 

  

A1095 Cars on Campus (30 分)

原文:https://www.cnblogs.com/moranzju/p/11100082.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!