对于凌乱的车辆进出,我们要把车牌号一样的放一起,再把车牌号一样的车的时间(不管进出)从小到大排好序, 遍历这个车牌的车的时候 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. 所以只有前in后out,才是有效数据,提取的时候顺便map记录时间 //注意:一辆车在一天内有可能多次进出。 提取出来的所有车辆按时间排序,用book记录该时间下的车辆数,下一车辆的数目是由上一上一状态转移的。 测试点4运行超时,当检查某时间车辆数目时,因为检查时间是增序,所以不必每次从头到尾去检查m*n,用tempindex标记当前位置节约时间
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define lowbit(i)((i)&(-i))
using namespace std;
typedef long long ll;
const int MAX=100005;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
const int SQR=633;
int n,m;
struct node
{
string id;
int flag;
int time;
}num;
int book[MAX];
vector<node>vt;
vector<node>car;
map<string,int>mp;
bool cmp(node a,node b)
{
if(a.id!=b.id)
return a.id<b.id;
if(a.time!=b.time)
return a.time<b.time;
}
bool cmp2(node a,node b)
{
return a.time<b.time;
}
int main()
{
memset(book,0,sizeof(book));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
string id,d;
int h,m,s,time;
node temp;
cin>>id;
scanf("%d:%d:%d",&h,&m,&s);
time=h*3600+m*60+s;
temp.time=time;
temp.id=id;
cin>>d;
if(d=="in")
temp.flag=1;
else if(d=="out")
temp.flag=-1;
vt.push_back(temp);
}
sort(vt.begin(),vt.end(),cmp);
int maxtcar=0;
for(int i=0;i<vt.size()-1;i++)
{
if(vt[i+1].flag==-1&&vt[i].flag==1&&vt[i+1].id==vt[i].id)
{
car.push_back(vt[i]);
car.push_back(vt[i+1]);
mp[vt[i].id]+=vt[i+1].time-vt[i].time;
if(maxtcar<mp[vt[i].id])
maxtcar=mp[vt[i].id];
}
}
sort(car.begin(),car.end(),cmp2);
for(int i=0;i<car.size();i++)
{
if(i==0)
book[i]=1;
else
book[i]=book[i-1]+car[i].flag;
}
int tempindex=0;
for(int i=0;i<m;i++)
{
int h2,m2,s2,check,j;
scanf("%d:%d:%d",&h2,&m2,&s2);
check=h2*3600+m2*60+s2;
for(j=tempindex;j<car.size();j++)
{
if(check<car[j].time)
{
printf("%d\n",book[j-1]);
break;
}
else if(j==car.size()-1)
{
printf("%d\n",book[j]);
}
}
tempindex=j;
}
for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++)
{
if(it->second==maxtcar)
printf("%s ",it->first.c_str());
}
printf("%02d:%02d:%02d",maxtcar/3600,maxtcar/60%60,maxtcar%60);
return 0;
}