#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
map<string,int> maxtime;
struct Car
{
int time;
char id[10];
bool state;
}car[10010],valid[10010];
bool cmp1(Car a,Car b)
{
int s=strcmp(a.id,b.id);
if(s!=0)
return s<0;
return a.time<b.time;
}
bool cmp2(Car a,Car b)
{
return a.time<b.time;
}
int main()
{
int n,k,mostlong=-1;
scanf("%d%d",&n,&k);
char line[5];
int hh,mm,ss;
for(int i=0;i<n;i++)
{
scanf("%s %d:%d:%d %s",car[i].id,&hh,&mm,&ss,line);
car[i].time=hh*3600+mm*60+ss;
car[i].state=strcmp(line,"in")==0?true:false;
}
//按照车牌号排序
sort(car,car+n,cmp1);
int num=0;
for(int i=0;i<n-1;i++)
{
//车牌号相同且前一个为in,后一个为out
if(!strcmp(car[i].id,car[i+1].id)&&car[i].state==true&&car[i+1].state==false)
{
//加入valid
valid[num++]=car[i];
valid[num++]=car[i+1];
//车牌号之前未出现,初始化为0
if(maxtime.count(car[i].id)==0)
maxtime[car[i].id]=0;
//累加停车时间
maxtime[car[i].id]+=car[i+1].time-car[i].time;
//更新停车时间最长车牌号
if(mostlong<maxtime[car[i].id])
mostlong=maxtime[car[i].id];
}
}
sort(valid,valid+num,cmp2);
for(int i=0;i<k;i++)
{
int ans=0;
scanf("%d:%d:%d",&hh,&mm,&ss);
int querytime=hh*3600+mm*60+ss;
//计算确定时间点停靠的车辆数,注意写法,为true,则ans++,反之ans--
for(int j=0;j<num&&valid[j].time<=querytime;j++)
valid[j].state==true?ans++:ans--;
printf("%d\n",ans);
}
map<string,int>::iterator it;
//输出停靠时间最长的车牌号,可能不止一个
for(it=maxtime.begin();it!=maxtime.end();it++)
{
if(it->second==mostlong)
printf("%s ",it->first.c_str());
}
printf("%02d:%02d:%02d\n",mostlong/3600,mostlong%3600/60,mostlong%60);
return 0;
}
PAT 1095 Cars on Campus (30 分)
最新推荐文章于 2022-09-10 20:50:29 发布