PAT(Advanced) 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.

解答:这道题和1016 Phone Bills (25)真的一模一样,只是多了要输出时间最长的车辆一个条件。总之,(1).先将错误的记录给删掉,(2)然后分别统计每个车辆的进出信息,(3)最后得到结果。思路还是比较清晰的。 

AC代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<set>

using namespace std;

typedef struct{
	string id;
	int hour, min, sec, time, status;
}car;

bool cmp(car c1, car c2)
{
	if(c1.id != c2.id){
		return ( c1.id < c2.id );
	}else if(c1.time != c2.time){
		return c1.time < c2.time;
	}else{
		return c1.status > c2.status;
	}
}

int counts[24*60*60+5];
int total[24*60*60+5];

int main()
{
	int N, K;
	
	memset(counts, 0, sizeof(counts));
	memset(total, 0, sizeof(total));
	scanf("%d %d", &N, &K);
	vector<car> cars(N);
	for(int i = 0; i < N; ++i)
	{
		char status[5];
		cin >> cars[i].id;
		scanf("%d:%d:%d %s", &cars[i].hour, &cars[i].min, &cars[i].sec, status);
		cars[i].status = strcmp(status, "in") == 0 ? 1 : 0;
		cars[i].time = cars[i].hour*60*60 + cars[i].min*60 + cars[i].sec;
	}
	sort(cars.begin(), cars.end(), cmp);
	
	map<string, vector<car> > carsmap;
	map<string, int> times;
	int maxtime = 0;
	
	for(int i = 1; i < N; ++i)
	{
		if(cars[i].id == cars[i-1].id && cars[i].status == 0 && cars[i-1].status == 1)
		{
			carsmap[cars[i-1].id].push_back(cars[i-1]);
			carsmap[cars[i].id].push_back(cars[i]);
			counts[cars[i].time]--;
			counts[cars[i-1].time]++;
			times[cars[i].id] += cars[i].time - cars[i-1].time;
			if(times[cars[i].id] > maxtime) maxtime = times[cars[i].id];
			//cout << cars[i-1].time << " "<< cars[i].time << endl;
		}
	}
	total[0] = counts[0];
	for(int i = 1; i < 24*60*60; ++i)
	{
		total[i] = total[i-1] + counts[i];
		//if(counts[i] != 0) cout << i << " " << counts[i] << endl;
	}
	//输出 
	int hour, min, sec;
	for(int i = 0; i < K; ++i)
	{
		scanf("%d:%d:%d", &hour, &min, &sec);
		printf("%d\n", total[hour*60*60 + min*60 + sec]);
	} 
	set<string> ids;
	for(map<string, int>::iterator iter = times.begin(); iter != times.end(); ++iter)
	{
		if(iter->second == maxtime) ids.insert(iter->first);
	}
	for(set<string>::iterator iter = ids.begin(); iter != ids.end(); ++iter)
	{
		cout << *iter <<" ";
	}
	int h = maxtime / (60*60) % 24, m = maxtime / 60 % 60, s = maxtime % 60;
	printf("%02d:%02d:%02d\n", h, m, s);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值