一、题目描述
原题链接
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:
Output Specification:
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
二、解题思路
有点复杂的一道模拟题,给我们若干个停车记录,要我们输出给出时间点对应的停车场的车的数目以及停车最久的车辆。我们可以把每一个停车记录设置为一个结构体,先把有效的停车记录筛选出来,之后对于每条停车记录,用一个map记录每辆车的停车时间,将停车时间最长的存在ans中。对于每个停车记录,我们可以计算当时的车总数,然后把题目给的时间作为限制条件,遍历有效的停车记录,找到第一个时间比题目给的query大的记录,输出车的总数即可。比较复杂,笔者表达能力有点弱,详情请看代码注释。
三、AC代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxt = 10000;
int cntT[maxt] = {0}; //表示每一个停车记录对应的停车场车的总数目
int maxTime = 0;
struct Car
{
char plate[10]; //车牌
int time[2] = {-1, -1} ; //进停车场的时间,出去的时间
int status; //in or out
};
vector<Car> all, valid;
int change(int h, int m, int s) //时间全部改成以s为单位
{
return h*3600 + m*60 + s;
}
void origin(int time)
{
int h, m, s;
s = time%60;
h = time/3600;
m = time%3600 / 60;
printf("%02d:%02d:%02d", h, m, s);
}
bool cmp(Car a, Car b) //比较函数,把同一辆车的停车记录通过排序整理出来
{
if(strcmp(a.plate, b.plate) != 0) return strcmp(a.plate, b.plate) < 0;
else return a.time[a.status] < b.time[b.status];
}
bool cmp2(Car a, Car b)
{return a.time[a.status] < b.time[b.status];}
int main()
{
int N, K, h, m, s;
Car tmp;
map<string, int> pktime; //存放每辆车的停车时间
vector<string> ans;
char ch[5];
scanf("%d%d", &N, &K);
for(int i=0; i<N; i++)
{
scanf("%s %d:%d:%d %s", tmp.plate, &h, &m, &s, ch);
tmp.status = strcmp(ch, "in") == 0 ? 1 : 0; //1是in,0是out
tmp.time[tmp.status] = change(h, m, s);
all.push_back(tmp);
}
sort(all.begin(), all.end(), cmp);
for(int i=0; i<all.size()-1; i++)
{
if(strcmp(all[i].plate, all[i+1].plate)==0 && all[i].status == 1 && all[i+1].status == 0) //一进一出才为有效
{
valid.push_back(all[i]);
valid.push_back(all[i+1]);
}
}
for(int i=0; i<valid.size(); i+=2)
{
pktime[valid[i].plate] += valid[i+1].time[0] - valid[i].time[1]; //计算停车时间
if(pktime[valid[i].plate] > maxTime)
{
ans.clear();
maxTime = pktime[valid[i].plate];
ans.push_back(valid[i].plate);
}
else if(pktime[valid[i].plate] == maxTime)
ans.push_back(valid[i].plate);
}
sort(valid.begin(), valid.end(), cmp2);
for(int i=0; i<valid.size(); i++) //更新cntT数组
{
if(i==0) cntT[i]++;
else cntT[i] = valid[i].status==0 ? cntT[i-1] - 1 : cntT[i-1] + 1; //如果进停车场,就+1,出停车场,就-1
}
int tmpidx = 0;
for(int i=0; i<K; i++)
{
scanf("%d:%d:%d", &h, &m, &s);
int tmptime = change(h, m, s);
int j;
for(j=tmpidx; j<valid.size(); j++)
{
if(valid[j].time[valid[j].status] > tmptime) //找到第一个时间大于query的记录
{
if(j == 0) printf("0\n");
else
printf("%d\n", cntT[j-1]);
break;
}
else if(j == valid.size()-1) printf("%d\n", cntT[j]);
}
tmpidx = j; //因为题目是按时间顺序给出的query,所以直接将tmpidx设为j,可以节省时间
}
sort(ans.begin(), ans.end()); //按字母表顺序
for(int i=0; i<ans.size(); i++)
{
printf("%s ", ans[i].c_str());
}
origin(maxTime); //打印停车时间
return 0;
}