A1095. Cars on Campus (30) 24分版本,有两个案例超时

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 // hahaha.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <stdio.h>
  6 #include <iostream>
  7 #include <vector>
  8 #include <map>
  9 #include <string>
 10 #include <cstdio>
 11 #include <set>
 12 #include <algorithm>
 13 using namespace std;
 14 const int maxn =10010;
 15 int n,k;
 16 int numc=0;
 17 map<string,int> mp;
 18 map<int,string> mp2;
 19 struct rec{
 20     vector<int> in;
 21     vector<int> out;
 22     vector<int> ru;
 23     vector<int> chu;
 24     string plate;
 25 }car[maxn];
 26 
 27 int change(string a)
 28 {
 29     if(mp.find(a)!=mp.end())
 30     {
 31         return mp[a];
 32     }else
 33     {
 34         mp[a]=numc++;
 35         mp2[numc-1]=a;
 36         return mp[a];
 37     }
 38 }
 39 int time1(int h,int m,int s)
 40 {
 41     return h*3600+m*60+s;
 42 }
 43 
 44 
 45 int _tmain(int argc, _TCHAR* argv[])
 46 {
 47     scanf("%d %d",&n,&k);
 48     for(int i=0;i<n;i++)
 49     {
 50         string a;
 51         int h,m,s;
 52         string b;
 53         cin>>a;
 54         scanf("%d:%d:%d",&h,&m,&s);
 55         cin>>b;
 56 
 57         /*cout<<a<<endl;
 58         printf("%d:%d:%d
",h,m,s);
 59         cout<<b<<endl;
 60         */
 61         int id=change(a);
 62         car[id].plate=a;
 63         if(b=="in")
 64             car[id].in.push_back(time1(h,m,s));
 65         else
 66             car[id].out.push_back(time1(h,m,s));
 67     }
 68     for(int i=0;i<numc;i++)
 69     {
 70         sort(car[i].in.begin(),car[i].in.end());
 71         sort(car[i].out.begin(),car[i].out.end());
 72     }
 73      //处理不配对
 74      for(int i=0;i<numc;i++)
 75      {
 76          int p=0,q=0;
 77          while(p<car[i].in.size()&&q<car[i].out.size())
 78          {
 79              if(car[i].out[q]<car[i].in[p])//找到q
 80              {
 81                while(car[i].out[q]<car[i].in[p])
 82                {
 83                  q++;
 84                }
 85                
 86              }
 87              
 88              //对q寻找p;
 89             
 90              
 91                     while(car[i].in[p]<car[i].out[q])
 92                     {
 93                         p++;
 94                         if(p>=car[i].in.size())break;
 95                     }
 96                     p--;
 97              
 98              
 99             
100              car[i].ru.push_back(car[i].in[p]);
101              car[i].chu.push_back(car[i].out[q]);
102 
103              //p++,q++
104              p++;
105              q++;
106          }
107          
108      }
109     int timecount=0;
110     for(int ha=0;ha<k;ha++)
111     {
112         timecount=0;
113         int h,m,s;
114         scanf("%d:%d:%d",&h,&m,&s);
115         int time=time1(h,m,s);
116         for(int i=0;i<numc;i++)
117         {
118             for(int j=0;j<car[i].ru.size();j++)
119             {
120                 if(j<car[i].chu.size())
121                 {
122                     if(car[i].ru[j]<=time&&car[i].chu[j]>time)
123                     {
124                         timecount++;
125                         break;
126                     }
127                 }else
128                 {
129                    if(car[i].ru[j]<=time)
130                     {
131                         timecount++;
132                         break;
133                     }
134                 }
135 
136             }
137         }
138         printf("%d
",timecount);
139 
140     }
141     //求停留最长时间
142    set<string> name;
143    int max=0;
144     for(int i=0;i<numc;i++)
145     {
146         int tmp=0;
147         for(int j=0;j<car[i].ru.size();j++)
148         {
149              tmp=tmp+car[i].chu[j]-car[i].ru[j];
150         }
151         if(tmp>max)
152             {
153                 name.clear();
154                 max=tmp;
155                 name.insert(car[i].plate);
156             }else if(tmp==max)
157             {
158                 name.insert(car[i].plate);
159             }
160     }
161     for(set<string>::iterator it=name.begin();it!=name.end();it++)
162     {
163      cout<<*it<<" ";
164     }
165 
166      int h=max/3600;
167      int m=(max-h*3600)/60;
168      int s=max%60;
169      printf("%02d:%02d:%02d",h,m,s);
170     return 0;
171 }
原文地址:https://www.cnblogs.com/ligen/p/4340808.html