1095-- Cars on Campus (30)

本文深入解析了一种用于大学校园停车场管理的算法,通过处理车辆进出记录,实现了对任意时间点校园内停车数量的准确统计,并能找出停留时间最长的车辆。文章详细介绍了时间处理、车辆表示、排序和统计方法等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1006

感谢https://www.nowcoder.com/discuss/478对我的帮助和启发。

题目:

题目描述

  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.

输入描述:

  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.

输出描述:

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.

输入例子:

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、时间的处理:

将h:m:s的形式化为秒s进行存储,简化编程时的计算。

2、汽车的表示:

struct Car{
    string plate;    //车牌号
    int second;      //停留时间(秒)
    bool is_in;      //state is in(true) or out(false)    
};

3、为方便每次对一个车进行时间的累加,需要先将杂乱的车辆记录按照车牌排序(字典顺序),然后再对时间排序。具体排序函数如下:

bool Cmp(Car const &a, Car const &b) {
	if (a.plate < b.plate) 	//字典顺序小的在前面
		return true;
	else if (a.plate > b.plate)
		return false;
	//同一辆车,按照时间先后排序
	return a.seconds < b.seconds;
}

PS: sort()使用function

长见识了sort()还可以自定义排序函数,只要这个函数返回值为bool即可,然后就会根据这个函数对指定范围的元素进行排序。例如:

bool cmp(const node &a, const node &b) {
int c = strcmp(a.s, b.s);
    if (c < 0) {
        return true;
    }
    if (c > 0) {
        return false;
    }
    return a.t < b.t;
}

sort(v.begin(), v.end(), cmp);

4、输出有两个任务:对输入给出的每个时间点统计出该时间点校园里的停车数目;找出停留时间最长的车辆及其停留时间;

  对于第一个任务,注意到题目中时间为1天的限制,我们可以采用一个讨巧的方法:记录一天中的每个时间段的车辆的进出数目,这里我们用数组a[3600*24]来表示,若有一辆车在t时间in,则a[t]++;若有一辆车在t时间out,则a[t]--。最后针对某个时间点,将该点之前的所有时间段累加,即得到该时刻校园内的车辆总数。

代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

struct Car {
	string plate;    //车牌号
	int seconds;     //停留时间(秒)
	bool is_in;      //state is in(true) or out(false)    
};
const int M = 3600 * 24;	//一天的总秒数
int a[M];					//记录每一秒大门的出入车情况,in +1; out -1
vector<Car> records;		//统计每个记录的车辆状况
string state;				//in or out
vector<string> answers;		//输出值
int N, K;

void Input1(void) {		//还有一部分没读入
	cin >> N >> K;
	records.resize(N);
	for (int i = 0; i < N; i++) {
		cin >> records[i].plate;
		int h, m, s;
		scanf("%d:%d:%d", &h, &m, &s);		//这里涉及到忽略“:”,使用scanf入读更为方便
		records[i].seconds = h * 3600 + m * 60 + s;
		cin >> state;
		if (state == "in")	records[i].is_in = true;
		else records[i].is_in = false;
	}
}

bool Cmp(Car const &a, Car const &b) {
	if (a.plate < b.plate) 	//字典顺序小的在前面
		return true;
	else if (a.plate > b.plate)
		return false;
	//同一辆车,按照时间先后排序
	return a.seconds < b.seconds;
}

int main(void) {
	Input1();
	sort(records.begin(), records.end(), Cmp);		//对记录排序

	//遍历已排好序的所有记录,选出停留时间最长的车,同时统计每个时间点的车辆进出
	int longest = -1;	//车辆最长停留时间
	for (int i = 0; i < N; i++) {
		string carPlate = records[i].plate;		//之后i会变,这里先存下来
		int nextCarIndex = i;
		while (nextCarIndex < N && (records[i].plate == records[nextCarIndex].plate))
			nextCarIndex++;		//找到下一牌照的汽车的第一条记录下标
		//统计一辆车的累计停留时间并找到最长停留车辆
		int tempt = 0;
		while (true) {	//同一车辆按照时间排序,若连续In中最后一个In后紧接着不是本车的out,则这个In(该车的剩余的所有In记录)可以作废
			while (i < nextCarIndex && !records[i].is_in) i++;		//跳过in前的out
			if (i >= nextCarIndex)	break;
			while (i < nextCarIndex && records[i].is_in) i++;	
			int index_in = --i;
			while (i < nextCarIndex && records[i].is_in) i++;
			if (i >= nextCarIndex)	break;
			int index_out = i;
			a[records[index_in].seconds]++;
			a[records[index_out].seconds]--;
			tempt += records[index_out].seconds - records[index_in].seconds;
		}
		if (tempt > longest) {
			longest = tempt;
			answers.clear();
			answers.push_back(carPlate);
		}
		else if (tempt == longest)
			answers.push_back(carPlate);
	}
	for (int i = 1; i < M; i++)
		a[i] += a[i - 1];	//统计每个时刻内的停放车辆总数
	for (int i = 0; i < K; i++) {
		int x, y, z;
		scanf("%d:%d:%d", &x, &y, &z);
		cout << a[x * 3600 + y * 60 + z] << endl;
	}
	for (int i = 0; i < answers.size(); i++)
		cout << answers.at(i) << " ";
	printf("%02d:%02d:%02d\n", longest / 3600, longest % 3600 / 60, longest % 60);
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值