1095 Cars on Campus (30 分)
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 ( ≤ 1 0 4 ) N (≤10^4) N(≤104), the number of records, and K ( ≤ 8 × 1 0 4 ) K (≤8×10^4) K(≤8×104) 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
题意
给出若干车辆出入校园的时间戳,查询若干时刻校园内停车数量,最后要求输出停留时间最长的车标识,若有多个则按字符升序。
思路
模拟,越模越没底。有以下几个点可以注意。
- 时间戳可能有无效项:必须按时间顺序单向的匹配最近的
in
和out
。例如按时间顺序某辆车在7、8、9、18点in
;在10、11、12、20点out
。那么只有9 ~ 10以及18 ~ 20的记录是有效的。不能嵌套。 - 查询可以先打个表,用
cnt[i]
记录第i条时间戳后校园内的车数。如果第i+1
条戳是out
,车数=cnt[i]-1
,否则车数=cnt[i]+1
。查询时如果第i条时间戳大于查询时间,那么上一个状态cnt[i-1]
就是要查时间前的车辆数。 - 查询是按时间升序的,所以每次不必从第1个时间戳开始,记录上次查询停止的位置继续搜即可。
- 高效一点可以用结构体数组配合两种
cmp
的sort
原地操作,性能更佳。我这里用unordered_map和priority_queue,也能过测试点。至于默认的容器没测试。。。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <set>
#include <iomanip>
using namespace std;
int N,K,MAX_TIME;
struct Record{
int time,type;
Record(){time=type=0;}
Record(int TIME,int TYPE):time(TIME),type(TYPE){}
friend bool operator<(Record a,Record b){return a.time>b.time;}//时间小的在队列头
};
bool cmp(Record a,Record b){return a.time<b.time;}
unordered_map<string,priority_queue<Record>> allRec;//按车牌号记录输入的所有时间戳
set<string> MAX_PLA;
vector<Record> realRec;//合法的时间戳,按时间排列
int main(){
cin>>N>>K;
string plate,status;
int hh,mm,ss;
for(int i=0;i<N;i++){
cin>>plate;
cin>>hh;
getchar();
cin>>mm;
getchar();
cin>>ss;
cin>>status;
ss+=(hh*60+mm)*60;//反正不超过一天,用秒来存储时间
auto it=allRec.find(plate);
if(it==allRec.end()){
priority_queue<Record> q;
if(status=="in")
q.push(Record{ss,1});
else
q.push(Record{ss,-1});
allRec.insert(pair<string,priority_queue<Record>>(plate,q));
}else{
if(status=="in")
it->second.push(Record{ss,1});
else
it->second.push(Record{ss,-1});
}
}
for(auto &item:allRec){//从所有时间戳里找到合法的时间戳,方法是对于每个车标的记录队列,匹配按时间升序的连续一对io
Record r_1,r_2;
int sum = 0;
if(item.second.size()<2) continue;//这个车标甚至没有两个戳
r_1=item.second.top();
item.second.pop();
r_2=item.second.top();
item.second.pop();
if(r_1.type == 1 && r_2.type == -1){
sum+=r_2.time-r_1.time;
realRec.push_back(r_1);
realRec.push_back(r_2);
}
while(!item.second.empty()){
r_1=r_2;
r_2=item.second.top();
item.second.pop();
if(r_1.type == 1 && r_2.type == -1){
sum+=r_2.time-r_1.time;
realRec.push_back(r_1);
realRec.push_back(r_2);
}
}
if(sum>MAX_TIME){
MAX_TIME=sum;
MAX_PLA.clear();
MAX_PLA.insert(item.first);
}else if(sum==MAX_TIME){
MAX_PLA.insert(item.first);
}
}
sort(realRec.begin(),realRec.end(),cmp);//不分车标,只按时间顺序排列所有真记录。
vector<int>cnt(N);
cnt[0]=1;
for(int i=1;i<realRec.size();i++) cnt[i]=cnt[i-1]+realRec[i].type;
int j=0,tmptime=0;
for(int i=0;i<K;i++){
cin>>hh;
getchar();
cin>>mm;
getchar();
cin>>ss;
ss+=(hh*60+mm)*60;
for(j=tmptime;j<realRec.size();j++){
if(realRec[j].time>ss){
cout<<cnt[j-1]<<'\n';
break;
}else if(j==realRec.size()-1)
cout<<cnt[j]<<'\n';
}
tmptime=j;//下次直接从本次的位置继续查
}
for(const auto &item :MAX_PLA){
cout<<item<<" ";
}
int H=MAX_TIME/3600,M_=MAX_TIME%3600/60,S=MAX_TIME%60;
cout.width(2);
cout.fill('0');
cout<<setw(2)<<H<<':'<<setw(2)<<M_<<':'<<setw(2)<<S;
}