【PAT】A1095 Cars on Campus (30point(s))

本文介绍了一个校园停车管理系统的设计与实现,该系统能够处理大量车辆进出记录,实时计算校园内停车数量,并找出停车时间最长的车辆。系统采用时间转换、排序和查询优化等技术,确保了高效的数据处理能力。

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


Author: CHEN, Yue
Organization: 浙江大学
Time Limit: 300 ms
Memory Limit: 64 MB
Code Size Limit: 16 KB

A1095 Cars on Campus (30point(s))

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 (≤10^​4​​ ), the number of records, and K (≤8×10^​4​​ ) 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

Code

#include <bits/stdc++.h>
using namespace std;
typedef struct car{
	char id[10];
	int time,flag=0;
}car;
vector<string> result;
unordered_map<string, int> parktime;
int tos(int h, int m, int s){return h * 3600 + m * 60 + s;}
bool cmp(car a, car b){
	if (strcmp(a.id, b.id)) // 如果不相等
        return strcmp(a.id, b.id) < 0;
	else
        return a.time < b.time;
}
bool cmp1(car a, car b){
    return a.time < b.time;
}
int main(){
	int n, k, num = 0, maxtime = -1;
	int h, m, s;
	scanf("%d %d", &n, &k);
	vector<car> cars(n),vtemp;
	for (int i = 0; i < n; i++){
        char status[5];
		scanf("%s %d:%d:%d %s", cars[i].id, &h, &m, &s, status);
		if(!strcmp(status,"in")) cars[i].flag=1;
        else    cars[i].flag=-1;
		cars[i].time = tos(h, m, s);
	}
	sort(cars.begin(), cars.end(), cmp);
	int nowtime = 0, index = 0;
	for (int i = 0; i < cars.size()-1; i++){
        if(!strcmp(cars[i].id,cars[i+1].id) && cars[i].flag==1&&cars[i+1].flag==-1){
            parktime[cars[i].id] += cars[i+1].time-cars[i].time;
            vtemp.push_back(cars[i]);
            vtemp.push_back(cars[i+1]);
        }
	}
    sort(vtemp.begin(), vtemp.end(), cmp1);
    vector<int> carnow(n);
    for(int i = 0; i < vtemp.size(); i++) {
        if(i == 0)  carnow[i] += vtemp[i].flag;
        else    carnow[i] = carnow[i-1] + vtemp[i].flag;
    }
	for (int i = 0; i < k; i++){
		scanf("%d:%d:%d", &h, &m, &s);
		nowtime = tos(h, m, s);
		int j;
		for(j=index;j<vtemp.size();j++){
            if(vtemp[j].time>nowtime){
                printf("%d\n",carnow[j-1]);
                break;
            }
            else if(j==vtemp.size()-1){
                printf("%d\n",carnow[j]);
            }
		}
		index=j;
	}
	for (unordered_map<string, int>::iterator it = parktime.begin(); it != parktime.end(); it++){
		if(it->second>maxtime){
            maxtime=it->second;
            result.clear();
            result.push_back(it->first);
		}
		else if(it->second == maxtime)   result.push_back(it->first);
	}
	sort(result.begin(),result.end());
	for(int i=0;i<result.size();i++)    printf("%s ", result[i].c_str());
	printf("%02d:%02d:%02d", maxtime / 3600, maxtime % 3600 / 60, maxtime % 60);
	return 0;
}

Analysis

-对于此类题,最好将时间全部转化为秒(统一单位)。

-给出n辆车的车牌号,时间,状态(出/入)。再给出k个时间点,要求每个时间点校园内有几辆车。最后输出本日在校园内停车时间最长的车的车牌号,多辆车按车牌字母顺序排列,以及停车时长。

《编译原理》是计算机科学中一门极为重要的课程,主要探讨如何将高级程序设计语言转换成机器可执行的指令。清华大学的张素琴教授在这一领域有着深厚的学术造诣,其编译原理课后习题答案对于学习者而言是非常珍贵的资源。这份压缩文件详细解析了课程中所涉及的概念、理论和方法的实践应用,目的是帮助学生更好地理解编译器设计的核心内容。 编译原理的核心知识点主要包括以下几点: 词法分析:作为编译过程的首要环节,词法分析器会扫描源代码,识别出一个个称为“标记”(Token)的最小语法单位。通常借助正则表达式来定义各种标记的模式。 语法分析:基于词法分析产生的标记流,语法分析器依据文法规则构建语法树。上下文无关文法(CFG)是编译器设计中常用的一种形式化工具。 语义分析:这一步骤用于理解程序的意义,确保程序符合语言的语义规则。语义分析可分为静态语义分析和动态语义分析,前者主要检查类型匹配、变量声明等内容,后者则关注运行时的行为。 中间代码生成:编译器通常会生成一种高级的中间表示,如三地址码或抽象语法树,以便于后续的优化和目标代码生成。 代码优化:通过消除冗余计算、改进数据布局等方式提升程序的执行效率,同时不改变程序的语义。 目标代码生成:根据中间代码生成特定机器架构的目标代码,这一阶段需要考虑指令集体系结构、寄存器分配、跳转优化等问题。 链接:将编译后的模块进行合并,解决外部引用,最终形成一个可执行文件。 错误处理:在词法分析、语法分析和语义分析过程中,编译器需要能够检测并报告错误,例如语法错误、类型错误等。 张素琴教授的课后习题答案覆盖了上述所有核心知识点,并可能包含实际编程练习,比如实现简单的编译器或解释器,以及针对特定问题的解题策略。通过解答这些习题,学生可以加深对编译原理的理解,提升解决问题的能力,为今后参与编译器开发或软件工程实践奠定坚实的基础。这份资源不仅是学习编译原理的有力辅助材料,也是
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值