前言

今天不开心就撸了一道PAT的题。 所有的合集相关源码我都更新在gitee上了需要自取xingleigao/study - Gitee.com


题目描述

1095 Cars on Campus (30 分)https://pintia.cn/problem-sets/994805342720868352/problems/994805371602845696Zhejiang 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 (≤104), the number of records, and K (≤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.

输入示例

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

解题思路

题目理解

PAT A级别的所有题目都是英文,其实题主作为一个六级都没考过的人都能看懂,相信大家都能看懂吧,看不懂也没关系,经常出现的单词也不多,多看就好了.

停车问题,就是给你车辆的进出记录,然后需要你根据要求输出相应时间点的车辆数目。然后输出停车时间最长的车辆和时间。

有个坑点是in和out交替出现 如果同一辆车多个in出现按照最后一个出现的时间点算,其它无效。

相关思路

分为多个步骤

1.记录单个车辆的记录时间,这里将时间化为以秒计时会更好算一些。然后定义两个这样的结构体,一个记录和处理原始数据,另外一个记录有效数据,用于输出时间点车辆信息。

struct Car{char id[8];int time;char status[4];
}all[maxn],valid[maxn];

2.根据要求进行数据的读入。并按照id和时间进行排序

bool cmp(Car a, Car b){ //按照id、时间排序;if(strcmp(a.id,b.id))    return strcmp(a.id,b.id) < 0;else return a.time < b.time;
}

3.对数据进行处理,有效的存储valid结构体数组,计算停车时长,然后创建一个map来对id和停车时间做映射。

map<string, int> partTime;

4.对valid按时间进行排序,然后读入需要输出的时间点,从头到时间点进行遍历统计当前时间点存在的车辆数目。

    for(int i =0;i < k;++i){                //输出当前时刻的车数scanf("%d:%d:%d",&hh,&mm,&ss);int time = timeToInt(hh,mm,ss);while(now < num && valid[now].time <= time){if(!strcmp(valid[now].status,"in"))   numCar++;else   numCar--;now ++;}

完整代码实现

#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 10010;
struct Car{char id[8];int time;char status[4];
}all[maxn],valid[maxn];
int  num = 0;
map<string, int> partTime;
int timeToInt(int hh,int mm,int ss){return hh * 3600 + mm * 60 + ss;//使用s做单位计时
}
bool cmp(Car a, Car b){ //按照id、时间排序;if(strcmp(a.id,b.id))    return strcmp(a.id,b.id) < 0;else return a.time < b.time;
}
bool cmptime(Car a, Car b){ //按照时间排序;return a.time < b.time;
}
int main(){int n,k,hh,mm,ss;scanf("%d%d",&n,&k);for(int i = 0;i < n; ++i){      //读入记录scanf("%s%d:%d:%d%s",all[i].id,&hh,&mm,&ss,all[i].status);all[i].time = timeToInt(hh,mm,ss);}sort(all, all + n,cmp);//排序int maxTime = -1;//最长停留时间for(int i = 1;i < n;++i){if(!strcmp(all[i].id,all[i - 1].id) && !strcmp(all[i - 1].status,"in") && !strcmp(all[i].status,"out")){//是否满足条件valid[num++] = all[i - 1];valid[num++] = all[i];int inTime = all[i].time - all[i - 1].time;if(partTime.count(all[i].id) == 0)  partTime[all[i].id] = 0;    //无记录插入记录partTime[all[i].id] += inTime;                                  //计算总停车时间if(partTime[all[i].id] > maxTime)   maxTime = partTime[all[i].id]; //记录最大停留时间}}sort(valid,valid + num,cmptime);int now = 0,numCar = 0;for(int i =0;i < k;++i){                //输出当前时刻的车数scanf("%d:%d:%d",&hh,&mm,&ss);int time = timeToInt(hh,mm,ss);while(now < num && valid[now].time <= time){if(!strcmp(valid[now].status,"in"))   numCar++;else   numCar--;now ++;}printf("%d\n",numCar);}map<string, int>::iterator it;for(it = partTime.begin();it != partTime.end();it ++){if(it->second == maxTime)   printf("%s ",it->first.c_str());}printf("%02d:%02d:%02d\n",maxTime/3600,maxTime%3600/60,maxTime%60);return 0;
}

结果分析与总结

PAT官网所有测试点都通过了。

算法笔记这个题给的有点小坑啊,map完全没学就给了这么一道题,简直离谱。不过c++里面的这些好用的东西确实要好好看看了。

【算法笔记题解】PAT A.1095 Cars on Campus (30 分)相关推荐

  1. 【PAT甲级】1095 Cars on Campus (30分)

    解题过程的小记录,如有错误欢迎指出. 难度:四星(运行超时orz崩溃题) 小导航~ 题目分析 注意点 我的解题过程 思路 bug 代码 dalao的代码 借鉴点 题目分析 给出停车进出记录和查询时间. ...

  2. 1095 Cars on Campus (30 分)【难 / 模拟 未完成】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805371602845696

  3. 【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 ...

  4. 1095. Cars on Campus (30)

    看了几个别人写的,感觉这个还不错 http://blog.csdn.net/xyt8023y/article/details/48029443 写一次理解: 1.首先把这N条记录存储起来,按时间从小到 ...

  5. PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA、最低公共祖先

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:二叉搜索树的中序遍历是隐含给定的,它的中序遍历就是从小到大排列. 所以这道题先是根据给定的前序遍历和中序遍历,建树. 建树的时候需要用 ...

  6. 1095 Cars on Campus

    1095 Cars on Campus (第一次使用map) Input Specification: Each input file contains one test case. Each cas ...

  7. PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...

  8. PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:m是背包容量,a1,a2,....,ana_1,a_2,....,a_na1​,a2​,....,an​是n个物品,第i个物品的体积是 ...

  9. PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这是一个公共子序列的问题.但是有点变式,序列a和序列b不是完全等价的,序列a的每个元素可以对应多个相同元素,而且有些元素可以不使用.比 ...

最新文章

  1. Linux-6.5下 MariaDB-10基于percona-XtraBackup备份工具的原理及配置详解
  2. 2021-04-03生产中实体关系抽取一般采用什么方法?
  3. Java 技术篇 - 前端浏览器发送一次url请求后端ServerSocket接收到两次请求原因及解决方法,GET /favicon.ico HTTP/1.1问题处理
  4. 重启docker 服务命令
  5. android 第三方登录界面,Android App集成第三方登录与换肤指南
  6. python mpi多线程_使用 MPI for Python 并行化遗传算法
  7. (待补充)CSS进阶--flex布局
  8. Redis(八):Redis的复制(Master/Slave)
  9. contiki list 链表
  10. C++命名空间和头文件的关系 例如已经使用了#includestring,为什么还要 using std::string?...
  11. 关于工厂的应用——中国工人和美国工人的问题
  12. 大数据学习第一章:初识大数据
  13. CAD 关于打断和合并对象
  14. 【干货】9个网络故障排除经典案例,网工都得会
  15. java生成word,html文件并将内容保存至数据库 (http://blog.163.com/whs3727@126/blog/static/729915772007325112014115/)
  16. 多亏了这几款软件,我才能坚持写博客这么多年!
  17. python配置文件
  18. C语言解决猴子吃桃问题
  19. 一个有趣好玩的HTML网页
  20. 2022-2028年中国人力资源服务行业市场发展前景及投资风险评估报告

热门文章

  1. java中自加(++)和自减(--)运算符
  2. 财管U11 股利、回购、送股与股票分割 教材解读
  3. thinkcmf随记
  4. 笔记:python spark机器学习与hadoop大数据
  5. 这些话真的很风骚....
  6. 电磁场与仿真软件(23)
  7. 短信验证码接收不到原因分析
  8. 图片右侧文字垂直居中显示
  9. 期货隔夜总是跳空,怎么办?
  10. Bifrost 同步数据库实现微服务跨库数据同步