[Questions] AcWing 1585. Cars on campus

Posted by essexboy on Mon, 06 Sep 2021 19:35:40 +0200

1585. Cars on campus

Theme transmission: AcWing 1585. Cars on campus

There are 8 8 Eight school districts and many gates.

From each gate we can collect the license plate numbers of the cars entering and leaving the gate and the specific entry and exit times.

Now, given all your useful information, what you need to do is:

  1. For some queries, the number of cars on campus at the query time is calculated.
  2. At the end of the day, find the car that parks the longest on the campus.

Input Format

The first line contains two integers. N N N denotes the number of records. K K K represents the number of query moments.

Next N N N lines, each containing a record in the following format:

plate_number hh:mm:ss status

plate_number is a length of 7 7 A string containing only uppercase letters and numbers, hh:mm:ss in hours: minutes: seconds represents the time of day, the earliest time is 00:00:00, the latest time is 23:59:59, and the status is in or out.

Note that all records are generated in one day.

Each in record is paired with the next record of the same car in chronological order, provided that the record is out.

All in records that are not paired with out records and out records that are not paired with in records must be ignored.

Make sure that at least one car can be paired successfully and that no one car will enter or leave at the same time.

Use 24-hour recording time.

Next K K K rows, each containing a query moment, such as hh:mm:ss.

Note that query moments are given in ascending order.

Output Format

For each query, output the total number of cars parked on campus in one row.

Note: For each query moment, all vehicle entry and exit incidents at that time are processed before counting the number of cars parked on the campus.

The last line of output should give the license plate number of the car with the longest parking time and the corresponding length of time.

If such a car is not unique, output all the license plate numbers in dictionary order, and then the corresponding length of time, separated by spaces between the license plate numbers and the time.

Data Range

1 ≤ N ≤ 1 0 4 1 \le N \le 10^4 1≤N≤104 ,
1 ≤ K ≤ 8 × 1 0 4 1 \le K \le 8\times10^4 1≤K≤8×104

Input sample:

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

Output sample:

1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09

Ideas:

First the same car's record data is cleaned, leaving legitimate data, then the total number of cars is determined by counting status when querying, and finally the longest parking time is found by traversing.

Questions:

#include<bits/stdc++.h>

using namespace std;

struct Event
{
	int tm, status;
	
	bool operator< (const Event &t) const
	{
		return tm < t.tm;
	}
};

int get(vector<Event>& ets)
{
	int res = 0;
	for(int i = 0; i < ets.size(); i += 2)
		res += ets[i + 1].tm - ets[i].tm;
	
	return res;
}

int main()
{	
	int n, m;

	scanf("%d%d", &n, &m);
	unordered_map<string, vector<Event>> cars;
	
	for(int i = 0; i < n; i++)
	{
		char id[10], status[10];
		int hh, mm, ss;
		scanf("%s %d:%d:%d %s", id, &hh, &mm, &ss, status);
		int t = hh * 3600 + mm * 60 + ss;
		int s = 0;
		if(status[0] == 'o')
			s = 1;
		cars[id].push_back({t, s});
	}
	
	vector<Event> events;
	for(auto& item : cars)
	{
		auto& ets = item.second;
		sort(ets.begin(), ets.end());
		int k = 0;
		for(int i = 0; i < ets.size(); i++)
		{
			if(ets[i].status == 0)
			{
				if(i + 1 < ets.size() && ets[i + 1].status == 1)
				{
					ets[k++] = ets[i];
					ets[k++] = ets[i + 1];
					i++;
				}
			}
		}
		ets.erase(ets.begin() + k, ets.end());
		for(int i = 0; i < k; i++)
			events.push_back(ets[i]);
	}
	
	sort(events.begin(), events.end());
	
	int k = 0, sum = 0;
	while(m--)
	{
		int hh, mm, ss;
		scanf("%d:%d:%d", &hh, &mm, &ss);
		int t = hh * 3600 + mm * 60 + ss;
		
		while(k < events.size() && events[k].tm <= t)
		{
			if(events[k].status == 0)
				sum++;
			else
				sum--;
			k++;
		}
		
		printf("%d\n", sum);
	}
	
	int maxt = 0;
	for(auto& item : cars)
		maxt = max(maxt, get(item.second));
		
	vector<string> res;
	for(auto& item : cars)
		if(get(item.second) == maxt)
			res.push_back(item.first);
		
	sort(res.begin(), res.end());
	
	for(int i = 0; i < res.size(); i++)
		printf("%s ", res[i].c_str());
		
	printf("%02d:%02d:%02d\n", maxt / 3600, maxt % 3600 / 60, maxt % 60);

	return 0;
}

Topics: C++