PAT甲级——A1095 Cars on Campus
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 (≤), the number of records, and K (≤) 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
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct Node
{
string ID;
bool flag;//1是进,0是出
int time;
Node(string s, bool f, int t) :ID(s), flag(f), time(t) {}
};
vector<Node>Cars;
unordered_map<string, vector<Node>>map;//一辆车不止一个记录
vector<string>res;
int N, K, maxTime = ;
int main()
{
cin >> N >> K;
string ID, flag;
int h, m, s;
while (N--)
{
cin >> ID;
scanf("%d:%d:%d", &h, &m, &s);
cin >> flag;
map[ID].push_back(Node(ID, flag == "in", h * + m * + s));
}
for (auto ptr = map.begin(); ptr != map.end(); ++ptr)
{
sort(ptr->second.begin(), ptr->second.end(), [](Node a, Node b) {return a.time < b.time; });
int t = ;
for (int i = ; i < ptr->second.size()-; ++i)
{
if (ptr->second[i].flag == && ptr->second[i + ].flag == )//一进一出
{
Cars.push_back(ptr->second[i]);//记录合法时间
Cars.push_back(ptr->second[i + ]);//记录合法时间
t += ptr->second[i + ].time - ptr->second[i].time;//累加停车时间
}
}
if (t > maxTime)
{
maxTime = t;
res.clear();
res.push_back(ptr->first);
}
else if(t==maxTime)
res.push_back(ptr->first);
}
sort(Cars.begin(), Cars.end(), [](Node a, Node b) {return a.time < b.time; });
int num = , i = ;
while (K--)
{
scanf("%d:%d:%d", &h, &m, &s);
int time = h * + m * + s;
for (; i < Cars.size() && Cars[i].time <= time; ++i)
num += Cars[i].flag ? : -;//使用用-1表示该车开出去了
cout << num << endl;
}
sort(res.begin(), res.end());
for (auto v : res)
cout << v << " ";
printf("%02d:%02d:%02d\n", maxTime / , maxTime % / , maxTime % );
return ;
}
PAT甲级——A1095 Cars on Campus的更多相关文章
- PAT甲级1095. Cars on Campus
PAT甲级1095. Cars on Campus 题意: 浙江大学有6个校区和很多门.从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码.现在有了所有的信息,你应该在任何特定的时间点告诉在 ...
- PAT甲级——1095 Cars on Campus (排序、映射、字符串操作、题意理解)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93135047 1095 Cars on Campus (30 分 ...
- 【刷题-PAT】A1095 Cars on Campus (30 分)
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...
- A1095 Cars on Campus (30)(30 分)
A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...
- PAT A1095 Cars on Campus (30 分)——排序,时序,从头遍历会超时
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- A1095. Cars on Campus
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...
- A1095 Cars on Campus (30 分)
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- PAT_A1095#Cars on Campus
Source: PAT A1095 Cars on Campus (30 分) Description: Zhejiang University has 8 campuses and a lot of ...
- pat 甲级 Cars on Campus (30)
Cars on Campus (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard 题目描述 Zhejiang University ...
随机推荐
- 全球CMOS图像传感器厂商
近期,台湾地区的Yuanta Research发布报告,介绍了其对CMOS图像传感器(CIS)市场的看法,以及到2022年的前景预期. 从该研究报告可以看出,2018年全球CMOS图像传感器的市场规模 ...
- iOS开发系列-NSFileManager
NSFileManager NSFileManager类主要对文件和目录的操作(删除.修改.移动.复制等等).
- Dede没见过的漏洞
payload:plus/search.php?keyword=xxx&arrs1[]=99&arrs1[]=102&arrs1[]=103&arrs1[]=95&am ...
- MySQL数据库之DDL(数据定义语言)
1.MySQL数据库之DDL创建.删除.切换 (1)查看所有数据库 show databases: (2)切换数据库 use 数据库名: (3)创建数据库 create database 数据库名: ...
- rem换算公式
当前rem基准值=预设的基准值/设计稿宽度*当前设备的宽度
- Cats Transport
Cats Transport 现在有n座山,第i座山的坐标为\(d_i\),初始p个饲养员在山1,有m只猫,每只猫有一个属性\(h_i,t_i\)表示猫i 在\(h_i\)以及它在\(t_i\)时间后 ...
- vs2013x64&&qt5.7.1编译osg3.4.0&&osgEarth2.7
此文仅备忘: 1.安装VS2013_Cn_Ult 2.安装qt-opensource-windows-x86-msvc2013_64-5.7.1 设置环境变量QTDIR,并将其bin加入到path中. ...
- JVM之类加载过程
# 类的生命周期 1. 加载 loading2. 验证 verification3. 准备 preparation4. 解析 resoluation5. 初始化 initialization6. 使用 ...
- Sqlite多线程相关整理
Sqlite多线程相关整理 Sqlite With MultiThreads 什么是线程安全? 当多个线程访问某个方法时,不管你通过怎样的调用方式.或者说这些线程如何交替地执行,我们在主程序中不需要去 ...
- Xshell 排版错乱问题