模拟专题
1095 Cars on Campus
配对要求是,如果一个车多次进入未出,取最后一个值;如果一个车多次out未进入,取第一个值。
注意:一个车可能出入校园好多次,停车的时间应该取之和
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <unordered_map>
using namespace std;
const int N=10010;
const int MAXT=24*3600;
int n,k,hh,mm,ss,flag,num,maxv;
string p,st;
unordered_map<string,int>plates;
unordered_map<int,string>revplates;
vector<string>ansstr;
struct Car{
int tot,flag;
bool operator<(Car x)const{
return tot<x.tot;
}
};
vector<Car>cars[N];
int timeList[MAXT];
int main() {
scanf("%d%d",&n,&k);
for(int i=1;i<=n;++i){
cin>>p;
if(!plates.count(p)) plates[p]=++num;
revplates[plates[p]]=p;
scanf("%d:%d:%d",&hh,&mm,&ss);
int tot=hh*3600+mm*60+ss;
cin>>st;
if(st=="in") flag=1;
else flag=0;
cars[plates[p]].push_back({tot,flag});
}
for(int i=1;i<=num;++i){
sort(cars[i].begin(),cars[i].end());
int j=0,span=0;
while(j<cars[i].size()){
while(!cars[i][j].flag&&j<cars[i].size()) j++;
if(j>=cars[i].size()) break;
while(cars[i][j].flag&&j<cars[i].size()) j++;
if(j>=cars[i].size()) break;
//cars[i][j-1].flag==1,cars[i][j].flag==0
timeList[cars[i][j-1].tot]++;
timeList[cars[i][j].tot]--;
span+=cars[i][j].tot-cars[i][j-1].tot;
j++;
}
if(span>maxv){
ansstr.clear();
ansstr.push_back(revplates[i]);
maxv=span;
}else if(span==maxv){
ansstr.push_back(revplates[i]);
}
}
for(int i=1;i<MAXT;++i)
timeList[i]+=timeList[i-1];
sort(ansstr.begin(),ansstr.end());
while(k--){
scanf("%d:%d:%d",&hh,&mm,&ss);
int tot=hh*3600+mm*60+ss;
printf("%d\n",timeList[tot]);
}
for(int i=0;i<ansstr.size();++i)
cout<<ansstr[i]<<" ";
printf("%02d:%02d:%02d\n",maxv/3600,(maxv/60)%60,maxv%60);
return 0;
}