PAT-A | 1095 | Cars on Campus (score-19)

1095. Cars on Campus (30)

Zhejiang University has 6 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 (<= 10000), the number of records, and K (<= 80000) 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

//string version

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Node {
	friend bool operator<(const Node &lhs, const Node &rhs);
	string plate_number;
	string time;
	string state;
};
bool operator<(const Node &lhs, const Node &rhs)
{
	return lhs.time < rhs.time;
}

//in order to compare parking time
struct TimeNode {
	friend bool operator<(const TimeNode &lhs, const TimeNode &rhs);
	string plate_number;
	int ptime;

	TimeNode() {
		plate_number = "";
		ptime = 0;
	}
};
bool operator<(const TimeNode &lhs, const TimeNode &rhs)
{
	return lhs.plate_number < rhs.plate_number;
}
int main()
{
	int N, K;
	Node *record;
	string *query;

	//step 1
	cin >> N >> K;
	record = new Node[N + 1];
	query = new string[K];
	for (int i = 0; i != N; ++i) {
		cin >> record[i].plate_number >> record[i].time >> record[i].state;
	}
	//for (int i = 0; i != K; ++i) {
	//	cin >> query[i];
	//}

	sort(record, &record[N]);


	//step 2
	//find invalid record(ignore)
	for (int i = 0; i != N; ++i) {
		for (int j = i + 1; j != N; ++j) {
			if (record[i].plate_number == record[j].plate_number) {
				if (record[i].state == record[j].state) {
					if (record[i].state == "in") {
						record[i].state = "invalid";
					} else if (record[i].state == "out") {
						record[j].state = "invalid";
					}
				}
				break;
			}
		}
	}


	//step 3
	//query
	for (int i = 0; i != K; ++i) {
		cin >> query[i];
		int cnt = 0;
		for (int j = 0; j != N; ++j) {
			if (record[j].state != "invalid" && record[j].time <= query[i]) {
				if (record[j].state == "in") {
					++cnt;
				} else {//record[j].state == "out"
					--cnt;
				}
			} else if (record[j].time > query[i]) {
				break;
			}
		}
		cout << cnt << endl;
	}

	//debug
	//string a = "05:10:33", b = "04:09:59";
	//int a2s, b2s;//a to second, b to second
	//a2s = ((a[0] - '0') * 10 + a[1] - '0') * 3600 + ((a[3] - '0') * 10 + a[4] - '0') * 60 + ((a[6] - '0') * 10 + a[7] - '0');
	//b2s = ((b[0] - '0') * 10 + b[1] - '0') * 3600 + ((b[3] - '0') * 10 + b[4] - '0') * 60 + ((b[6] - '0') * 10 + b[7] - '0');
	//cout << a2s << " " << b2s << endl;

	//step4
	//count every car's parking time
	TimeNode *parktime = new TimeNode[N];
	int maxTime = 0;
	int k = 0;
	for (int i = 0; i != N; ++i) {
		if (record[i].state == "in") {
			int j;
			for (j = i + 1; j != N; ++j) {
				if (record[i].plate_number == record[j].plate_number &&
					record[j].state == "out") {
					string a = record[i].time, b = record[j].time;
					int a2s, b2s;//string time a to second, string time b to second
					a2s = ((a[0] - '0') * 10 + a[1] - '0') * 3600 + ((a[3] - '0') * 10 + a[4] - '0') * 60 + ((a[6] - '0') * 10 + a[7] - '0');
					b2s = ((b[0] - '0') * 10 + b[1] - '0') * 3600 + ((b[3] - '0') * 10 + b[4] - '0') * 60 + ((b[6] - '0') * 10 + b[7] - '0');
					//cout << a2s << " " << b2s << " " << b2s - a2s << endl;
					int m;
					for (m = 0; m < k; ++m) {
						if (parktime[m].plate_number == record[i].plate_number) {
							parktime[m].ptime += b2s - a2s;
							if (parktime[m].ptime > maxTime) {
								maxTime = parktime[m].ptime;
							}
							break;
						}
					}
					if (m >= k) {
						parktime[k].plate_number = record[i].plate_number;
						parktime[k].ptime = b2s - a2s;
						if (parktime[k].ptime > maxTime) {
							maxTime = parktime[k].ptime;
						}
						++k;
					}
					break;
				}
			}
		}
	}
	sort(parktime, &parktime[k]);

	for (int i = 0; i != k; ++i) {
		if (parktime[i].ptime == maxTime) {
			cout << parktime[i].plate_number << " ";
		}
	}

	int h, m, s;
	h = maxTime / 3600;
	m = maxTime % 3600 / 60;
	s = maxTime % 60;
	printf("%02d:%02d:%02d\n", h, m, s);
}



这个题没有做全对,只有sample(测例1)和测例4是答案正确,其他4个都超时了。

我把这个版本的string都改成了char *,依旧超时,问题的关键不在这里吧。猜测问题可能在两个结构体设置这里,可能有一个c++自带的数据结构能处理这里的问题,只是自己不熟练,想不到。


解题思路:

1. 结构体Node用来存输入的数据,包括车牌号、进出的时间、状态是in还是out,并且根据进出的时间对其排序。

2. 处理无效的数据。同一个车牌号,有连续的in,则时间靠前的无效(因为靠前的in之后没有一个out与其配对就遇见了靠后的in),把其状态改为invalid;如果有连续的out,则时间靠后的无效(靠后的out之前没有一个In与其配对,其之前只有靠前的out),把其状态改为invalid

3. 处理每一条查询。查询是按照时间来的,而之前已经根据时间对输入的记录排过序了,所以只需要从头到尾遍历输入,统计小于查询的时间点的车数就行了,遇见in时cnt++,遇见out时cnt--,遇见valid跳过。

4. 统计停车时间最长的车牌号以及停车时间。新建一个结构体TimeNode用来按车牌号对记录进行排序。在排序之前先统计每一辆车的总的停车时间,在统计的时候也找到最长的停车时间。从头到尾遍历输入Node[N],遇见in就开始找同车牌号的out,然后计算两个记录的时间差,放在TimeNode里。最后得到每辆车的总的停车时间。统计完之后按车牌号进行排序。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值