PAT 甲级 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.

输入

Each input file contains one test case. Each case starts with two positive integers N ( ≤ 1 0 4 ≤10^4 104), the number of records, and K ( ≤ 8 × 1 0 4 ≤8×10^4 8×104) 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.

输出

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.

思路

模拟题,首先是按照车牌进行排序然后匹配每一个in out。之后匹配完按照时间进行排序,统计个数就行

代码

#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct node {
	string car;
	int time;
	int flag;
}Record;
Record dirty[10010];
Record real[10010];
int number[100010] = { 0 };
int R = 0;
int mmax = 0;
vector<string> longest;
void add(int cur)
{
	real[R] = dirty[cur - 1];
	R++;
	real[R] = dirty[cur];
	R++;
}
bool cmp(Record a, Record b)
{
	if (a.car != b.car)
	{
		return a.car < b.car;
	}
	else
	{
		return a.time < b.time;
	}
}
bool cmp2(Record a, Record b)
{
	return a.time < b.time;
}
int main()
{
	int N, K;
	cin >> N >> K;
	for (int i = 0; i < N; i++)
	{
		cin >> dirty[i].car;
		int h, m, s;
		scanf("%d:%d:%d", &h, &m, &s);
		dirty[i].time = h * 3600 + m * 60 + s;
		char str[5];
		cin >> str;
		if (str[0] == 'i')
		{
			dirty[i].flag = 1;
		}
		else
		{
			dirty[i].flag = 0;
		}
	}
	sort(dirty, dirty + N, cmp);

	/*for (int i = 0; i < N; i++)
	{
		cout << dirty[i].car << " " << dirty[i].time << " " << dirty[i].flag << endl;
	}*/
	//cout << endl << endl;
	dirty[N].car = "";
	dirty[N].time = 0;
	dirty[N].flag = -1;
	int temp = 0;
	for (int i = 1; i <= N; i++)
	{
		if (dirty[i].car == dirty[i - 1].car)
		{
			if (dirty[i - 1].flag == 1 && dirty[i].flag == 0)
			{
				add(i);
				temp += dirty[i].time - dirty[i - 1].time;
			}
		}
		else
		{
			if ( temp> mmax)
			{
				mmax = temp;
				longest.clear();
				longest.push_back(dirty[i-1].car);
			}
			else if (temp == mmax)
			{
				longest.push_back(dirty[i - 1].car);
			}
			temp = 0;
		}
	}
	/*for (int i = 0; i < R; i++)
	{
		cout << real[i].car << " " << real[i].time << " " << real[i].flag << endl;
	}*/
	sort(real, real + R, cmp2);

	int cur = 0;
	for (int i = 0; i < 86400; i++)
	{
		number[i] = -1;
	}
	for (int i = 0; i < R; i++)
	{
		if (real[i].flag == 1)
		{
			cur++;
		}
		else
		{
			cur--;
		}
		number[real[i].time] = cur;
	}
	cur = 0;

	for (int i = 0; i < 86400; i++)
	{
		if (number[i] != -1)
		{
			cur = number[i];
		}
		number[i] = cur;
	}
	for (int i = 0; i < K; i++)
	{
		int h, m, s;
		scanf("%d:%d:%d", &h, &m, &s);
		int temp = h * 3600 + m * 60 + s;
		printf("%d\n", number[temp]);
	}
	for (int i = 0; i < longest.size(); i++)
	{
		cout << longest[i] << " ";
	}
	int h = mmax / 3600;
	int m = (mmax % 3600) / 60;
	int s = mmax % 60;
	printf("%02d:%02d:%02d\n", h, m, s);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值