1095 Cars on Campus (30 分)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_42582136/article/details/102637612

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:
Each input file contains one test case. Each case starts with two positive integers N (≤10
​4
​​ ), the number of records, and K (≤8×10
​4
​​ ) the number of queries. Then N lines follow, each gives a record in the format:

plate_number hh:mm:ss status
where plate_number is a string of 7 English capital letters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59; and status is either in or out.

Note that all times will be within a single day. 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. It is guaranteed that at least one car is well paired in the input, and no car is both in and out at the same moment. Times are recorded using a 24-hour clock.

Then K lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given in ascending order of the times.

Output Specification:
For each query, output in a line the total number of cars parking on campus. The last line of output is supposed to give the plate number of the car that has parked for the longest time period, and the corresponding time length. If such a car is not unique, then output all of their plate numbers in a line in alphabetical order, separated by a space.

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

这道题的逻辑很简单,包括最长时间的和查询停车数量处理都很容易。给定车辆的出入校园时间,给出查询,判断查询时间校园内共有多少车辆,最后给出停车时间最长的车的停车时间和车牌。

一开始我是被status的匹配问题难住了,主要是不清楚到底要怎样匹配
先把所有停车时间排序看到的是这样的:
在这里插入图片描述
可以看到车牌号为JH007BD的车辆出现了很多次,其实匹配规则是in和out就近匹配,就是说只有in的后面是out时这两个才算一对出入时间,其他的全都可以抛弃。
比如这里的
1 2  3  4 5  6
in in out out in out
就只有2 3 和5 6匹配

像这种情况下其实题目还是有提示的,比如两个停车时间都相同的两辆车就包括
JH007BD,计算一下时间就可以找到这个规则。

写了两种方式,第二种是把所有的记录都存起来,后面的操作全部储存的是下标。

#include<bits/stdc++.h>
using namespace std;
struct cars{
	string name, time, status;
};
int main()
{
	int n, m,ret=0;
	cin >> n >> m;
	cars c;
	vector<cars>car;
	map<string, vector<pair<string, string>>>park, calc_time;
	for (int i = 0; i < n; i++){
		cin >> c.name >> c.time >> c.status;
		park[c.name].push_back({ c.time,c.status });
	}
	for (auto& it : park){
		sort(it.second.begin(), it.second.end());
		for (int i = 0; i < it.second.size() - 1; i++)
			if (it.second[i].second == "in" && it.second[i + 1].second == "out"){
				car.push_back({ it.first,it.second[i].first,it.second[i].second });
				car.push_back({ it.first,it.second[i + 1].first,it.second[i + 1].second });
				calc_time[it.first].push_back({ it.second[i].first,it.second[i].second });
				calc_time[it.first].push_back({ it.second[i + 1].first,it.second[i + 1].second });
			}
    }
	sort(car.begin(), car.end(), [](const cars& a, const cars& b) {return a.time < b.time; });
	vector<int>time_(car.size(),1);
	for (int i = 1; i < car.size(); i++)
		time_[i] = time_[i - 1] + (car[i].status == "in" ? 1 : -1);
	set<string>longest_time;
#define f(x) (stoi(x.substr(0, 2)) * 3600 + stoi(x.substr(3, 2)) * 60 + stoi(x.substr(6, 2)))
	for (auto& it : calc_time){
		int tmp = 0;
		for (int j = 0; j < it.second.size(); j += 2)
			tmp += f(it.second[j + 1].first) - f(it.second[j].first);
        if(tmp<ret) continue;
		if (tmp > ret){
			ret = tmp;
			longest_time.clear();
		}
		longest_time.insert(it.first);
	}
	string query;
	for (int i = 0, j = 0; i < m; i++){
		cin >> query;
		for (; j < time_.size(); j++)
			if (car[j].time > query){
				cout << time_[j - 1] << endl;
				break;
			}
		if (j >= time_.size())
			cout << 0 << endl;
	}
	for (auto& it : longest_time) printf("%s ", it.c_str());
	printf("%02d:%02d:%02d", ret / 3600, ret / 60 % 60, ret % 60);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct cars{
	string name, time, status;
};
int main()
{
	int n, m, ret = 0;
	cin >> n >> m;
	cars c;
	vector<cars>car;//输入的全部记录
	map<string, vector<int>>park, calc_time;
	vector<int>val_car;//有效数据
	for (int i = 0; i < n; i++){
		cin >> c.name >> c.time >> c.status;
		car.push_back(c);
		park[c.name].push_back(i);
	}
	for (auto& it : park){
		sort(it.second.begin(), it.second.end(), [&](int& a, int& b) {
			return car[a].time < car[b].time; });
		for (int i = 0; i < it.second.size() - 1; i++)
			if (car[it.second[i]].status == "in" && car[it.second[i + 1]].status == "out"){
				val_car.push_back(it.second[i]);
				val_car.push_back(it.second[i + 1]);
				calc_time[car[it.second[i]].name].push_back(it.second[i]);
				calc_time[car[it.second[i]].name].push_back(it.second[i + 1]);
			}
    }
	sort(val_car.begin(), val_car.end(), [&](int& a, int& b) {
		return car[a].time < car[b].time; });
	vector<int>total(val_car.size(), 1);
	for (int i = 1; i < val_car.size(); i++)
		total[i] = total[i - 1] + (car[val_car[i]].status == "in" ? 1 : -1);
	set<string>longest_time;
#define f(x) (stoi(x.substr(0, 2)) * 3600 + stoi(x.substr(3, 2)) * 60 + stoi(x.substr(6, 2)))
	for (auto& it : calc_time){
		int tmp = 0;
		for (int j = 0; j < it.second.size(); j += 2)
			tmp += f(car[it.second[j + 1]].time) - f(car[it.second[j]].time);
		if (tmp < ret) continue;
		if (tmp > ret){
			ret = tmp;
			longest_time.clear();
		}
		longest_time.insert(it.first);
	}
	string query;
	for (int i = 0, j = 0; i < m; i++)
	{
		cin >> query;
		for (; j < total.size() && car[val_car[j]].time <= query; j++);
		if(j<total.size()) printf("%d\n", total[j - 1]);
		else if (j >= total.size()) printf("0\n");
	}
	for (auto& it : longest_time) printf("%s ", it.c_str());
	printf("%02d:%02d:%02d", ret / 3600, ret / 60 % 60, ret % 60);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42582136/article/details/102637612