注释已经很详细了,我只想说有个坑,同一时间进车出车正好要进行输出,此时是先进出车后再计算车的数量进行输出的
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#pragma warning(disable :4996)
using namespace std;
struct node {//时间轴的节点
int t;//时间
int flag;//0代表出,1代表进,-1代表输出泊车数目
node() = default;
node(int x, int f) { flag = f;t = x;}
bool operator<(const node b) const
{
return t < b.t ||
(t==b.t && flag>b.flag);
}
};
struct nnode {//保存输入的节点
string name;//车牌号
int t;//时间
int flag;//0代表出,1代表进,-1代表输出泊车数目
nnode() = default;
nnode(int x, int f, string s) { flag = f;t = x;name = s; }
bool operator<(const nnode b) const
{
return name < b.name ||
(name == b.name&&t < b.t);
}
};
int main()
{
map < string, int> Tim;//保存车停的时间
vector<nnode> all;//所有N的输入
vector<node> timestamp;//时间轴
vector<string> re;//保存最长时间输出的车辆
int M=0;//保存最长泊车时间
int N, K;
cin >> N >> K;
while (N--)//保存所有的输入
{
int a, b, c;
char s1[100], s2[100];
scanf("%s %d:%d:%d %s", s1, &a, &b, &c, s2);
a = a * 3600 + b * 60 + c;
if (s2[0] == 'i')
all.push_back(nnode(a, 1,string(s1)));
else
all.push_back(nnode(a, 0,string(s1)));
}
sort(all.begin(), all.end());//按车牌号排序,同一车牌号按时间排序
for (int t = 1;t < all.size();t++)
{
if (all[t].name == all[t - 1].name && all[t - 1].flag == 1 && all[t].flag == 0)//筛选有用的信息并进行处理
{
timestamp.push_back(node(all[t - 1].t, 1));
timestamp.push_back(node(all[t].t, 0));
Tim[all[t].name] += all[t].t - all[t - 1].t;
t++;//跳两下
}
}
while (K--)
{
int a, b, c;
scanf("%d:%d:%d", &a, &b, &c);
a = a * 3600 + b * 60 + c;
timestamp.push_back(node(a, -1));//-1代表输出
}
sort(timestamp.begin(), timestamp.end());//对时间轴进行排序
int num = 0;//代表当前泊车数
for (auto x : timestamp)
{
if (x.flag == 0)--num;
else if (x.flag == 1)++num;
else printf("%d\n", num);
}
M = 0;
for (auto x : Tim)//寻找最大泊车时间
{
if (x.second > M)
{
re.clear();
re.push_back(x.first);
M = x.second;
}
else if (x.second == M) re.push_back(x.first);
}
sort(re.begin(), re.end());//对最大泊车时间的几辆车排序
for (auto x : re)
printf("%s ",x.c_str());
printf("%02d:%02d:%02d\n", M / 3600, M % 3600 / 60, M % 60);//输出时间
}