PAT甲级1095 Cars on Campus (30 分)

1095 Cars on Campus (30 )

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 (104), the number of records, and K (8×104) the number of queries. Then Nlines 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 inrecords 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

      题目大意:

         给出n个记录以及m个查询,每次查询都给出一个时间,查询截止当时还存在停车场里的车辆数量,最后输出当天停车最久的车。

 

 

思路:

         注意给出的记录是需要排查的,会存在无效记录,只有相邻的in 与out才有效。纠正好记录后,按时间排好,由于查询的时间是递增的,那么记录当时还在校园内的车辆个数cnt是可以继承的。每进一辆车就加一,出一辆车就减一无论是哪一辆。

         在计算每辆车的停靠时间时用到map结构,在计算停靠时间时同时把最大停靠时间与相应的车计算好。

 

参考代码:

 

 

#include<map>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	string id;
	int t, flag;
};
bool cmp1(node a, node b){
	if(a.id != b.id)	return a.id < b.id;
	else	return a.t < b.t;
}
bool cmp2(node a, node b){return a.t < b.t;}
vector<node> ans, list;
map<string, int > cnt;
int hh, mm, ss;
int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i){
		int flag;
		char id[8], s[4];
		scanf("%s %d:%d:%d %s", id, &hh, &mm, &ss, s);
		if(strlen(s) == 2)	flag = 1;
		else	flag = 2;
		hh = hh * 3600 + mm * 60 + ss;
		list.push_back({id, hh, flag}); 
	}
	sort(list.begin(), list.end(), cmp1);
	int maxt = -1;
	for(int i = 0; i < n - 1; ++i)
		if(list[i].flag == 1 && list[i + 1].flag == 2 && list[i].id == list[i + 1].id){
			ans.push_back(list[i]), ans.push_back(list[i + 1]);
			cnt[list[i].id] += list[i + 1].t - list[i].t;
			maxt = max(maxt, cnt[list[i].id]);
		}
	sort(ans.begin(), ans.end(), cmp2);
	int sum = 0, j = 0;
	for(int i = 0; i < m; ++i){
		scanf("%d:%d:%d", &hh, &mm, &ss);
		hh = hh *3600 + mm * 60 + ss;
		while(j < ans.size() && ans[j].t <= hh){
			if(ans[j].flag == 1)	sum++;
			else	sum--;
			j++;
		}
		printf("%d\n", sum);
	}
	for(auto it = cnt.begin(); it != cnt.end(); ++it)
		if(it->second == maxt)	printf("%s ", it->first.c_str());
	printf("%02d:%02d:%02d", maxt / 3600, maxt % 3600 / 60, maxt % 60);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值