浙大pat | 浙大pat 牛客网甲级 1006 Cars on Campus (30)多区域覆盖题

题目描述

Zhejiang University has 6 campuses and a lot of gates.  From each gate we can collect the in/outtimes and the plate numbers of the cars crossing the gate.  Now with all the information available, youare supposed to tell, at any specific time point, the number of cars parking oncampus, and at the end of the day find the cars that have parked for thelongest time period.



输入描述:

Each input file contains one test case.  Each case starts with two positive integers N(<= 10000), the number of records, and K (<= 80000) the number ofqueries.  Then N lines follow, each givesa record in the format
plate_number hh:mm:ss status
where
plate_number is a string of 7 English capitalletters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, withthe 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 ispaired 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 pairedwith an "in" record. It is guaranteed that at least one car is wellpaired in the input, and no car is both "in" and "out" atthe 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.




输出描述:

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



输入例子:

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



输出例子:

1

4

5

2

1

0

1

JH007BD ZD00001 07:20:09

这一题就是一个区域覆盖求指定点覆盖一数的题目,但是条件和隐藏条件太多了

1)       一个汽车可能出现多次

2)       当存在一个汽车的多个匹配的时候,取最中间的一个匹配

3)       存在时间最长的汽车牌照需要按照字典升序排列

4)       当某个时间点一个汽车刚离开,在查询这个时间点的时候这个汽车需要计入总数

先来说说第一个条件,题目中并没有说明,但是一辆汽车真的可能出现多次,所以在计算的时候记得使用一个哈希表来存储每一个汽车在这个停车场里面待的累计的时间,第二个问题就有一些蛋疼了,这个问题我想了好久,我最开始以为题目中没有说明这种情况,那么这种情况就不会出现,但是在答题的时候发现这种情况是有可能出现的,并且默认取中最中间的匹配,所以以后在做这种匹配的题目的时候,题目中没有特别说明这样匹配的情况下,取最中间的匹配

还有就是在做区间题的时候,同一个区间可能出现多次,一定要记住,比如这一题,同一辆汽车可能出现多次

按照字典升序排序直接使用sort就能够解决

这一题还用到了处理字符串时间的技巧,直接使用time1.compare(time2)能够直接得到两个时间的大小,但是比较时间区间的大小就不能这样了,需要把两个时间转换成单位为秒的数值然后比较大小!求时间差也必须这样做!

还有就是以后再做多范围的题目的时候,题目中没有特别说明,这些范围的边界都该认为是在这个范围内部!

#include<iostream>
#include <unordered_map>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
unordered_map<string, int> everyOneStayTime;
struct node
{
	string plateNum;
	string _time;
	string statu;
	node (string plateNum2=0,string _time2=0,string statu2=0) : plateNum(plateNum2), _time(_time2),statu(statu2) {}
};
int theLongestTime = -1;
void getTheLongestTime(string theLater, string theBefore,string theName , vector<string> &theLongestName)
{
	if (theName == "JH007BD")
	{
		int j_makr = 1;
	}
	int time1[3];
	int time2[3];
	int tmp;
	time1[0] = atoi(theLater.substr(0, 2).c_str());
	time1[1] = atoi(theLater.substr(3, 2).c_str());
	time1[2] = atoi(theLater.substr(6, 2).c_str());

	time2[0] = atoi(theBefore.substr(0, 2).c_str());
	time2[1] = atoi(theBefore.substr(3, 2).c_str());
	time2[2] = atoi(theBefore.substr(6, 2).c_str());

	tmp = (time1[0] - time2[0]) * 3600 + (time1[1] - time2[1]) * 60 + (time1[2] - time2[2]);
	tmp = (everyOneStayTime[theName] += tmp);
	if (tmp > theLongestTime)
	{
		theLongestTime = tmp;
		theLongestName.clear();
		theLongestName.push_back(theName);
	}
	else if (tmp == theLongestTime)
	{
		theLongestName.push_back(theName);
	}
}
bool cmp(const node & a, const node  &b)
{
	return a._time.compare(b._time) < 0;
}
int main()
{
	int N, K;
	string a, b, c;
	vector<node> theRecord;
	
	map<string, int> theStand;
	cin >> N >> K;
	for (int i = 0; i < N; i++)
	{
		cin >> a >> b >> c;
		theRecord.push_back(node(a, b, c));
	}
	sort(theRecord.begin(), theRecord.end(), cmp);

	vector<string> theLongestName;
	for(int i=0;i<theRecord.size();i++)
		if(theRecord[i].statu=="in")
		for (int j = i + 1; j < theRecord.size(); j++)
		{
			if (theRecord[j].plateNum == theRecord[i].plateNum)
			{
				if (theRecord[j].statu == "out")
				{
					theStand[theRecord[i]._time]++;
					string yy = theRecord[i]._time;
					theStand[theRecord[j]._time]--;
					getTheLongestTime(theRecord[j]._time, theRecord[i]._time, theRecord[i].plateNum, theLongestName);
					break;
				}
				else
				{
					break;
				}
			}
		}
	
	int theCount = 0;
	map<string, int> ::iterator it2 = theStand.begin();
	sort(theLongestName.begin(), theLongestName.end());
	for (int i = 0; i < K; i++)
	{
		cin >> a;
		while (it2 != theStand.end() && it2->first.compare(a) <= 0)
		{
			theCount += it2->second;
			string  pp = it2->first;
			it2++;
		}
		cout << theCount << endl;
	}
	for (int i = 0; i < theLongestName.size(); i++)
		cout << theLongestName[i] << " ";

	if (theLongestTime / 3600 < 10) cout << 0;
	cout << theLongestTime / 3600<<":";
	theLongestTime %= 3600;

	if (theLongestTime / 60 < 10) cout << 0;
	cout << theLongestTime / 60<<":";
	theLongestTime %= 60;

	if (theLongestTime  < 10) cout << 0;
	cout << theLongestTime ;
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值