PAT 1095 Cars on Campus
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.
Input Specification:
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 accending 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
题目大意:需要统计在特定的时间在校园内停车的总数,并且找出本天停车时间最长的那辆车。输入数据有n个车辆进出信息,并有m个时间点查询。
每个车辆有车牌号 时间 状态,并且每个in都和下一个最近的out匹配,没有匹配的in或者out忽略掉。给定的查询时间是递增的。
//将时间都转换为int存储。每来一个就查询一次时间复杂度好高啊,8次方了,千万级别。
//但是如果有两个in,那么谁和第一个out对呢?那肯定是最后一个。
//思路错了,如果有一辆车in out in out你这种方法就不行了。
#include <iostream>
#include <vector>
#include <map>
#include <string.h>
#include<cstdio>
using namespace std;
struct Car{
char name[];
int in,out;
Car(char *n,int i,int o){
name=n;in=i;out=o;//这样易于后边的排出。
}
};
map<int,string> id2name;
map<string,int> name2id;
vector<Car> cars;
int main() {
int n,m;
scanf("%d%d",&m,&n);
char pl[],state[];
int h,m,s;
string nm;
for(int i=;i<n;i++){
scanf("%s %d:%d:%d %s",&pl,&h,&m,&s,&state);
nm=pl;
int tm=h*+m*+s;//总时间,从0开始计数。
if(name2id.count(nm)==&&state[]=='i'){//如果以前没出现过,而且不是out直接pass即可。
name2id[nm]=i;
id2name[i]=nm;
cars.push_back(Car(pl,tm,-));
}else if(name2id.count(nm)==){//如果已经又过了 }
} return ;
}
//写到这里写不下去了,不知道该如何处理,如何判断。
代码转自:https://www.liuchuo.net/archives/2951
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;
struct node {
char id[];
int time, flag = ;
};
bool cmp1(node a, node b) {
if(strcmp(a.id, b.id) != )
return strcmp(a.id, b.id) < ;//对所有的输入按照id排序。
else
return a.time < b.time;//同一辆车时间小的在前。
}
bool cmp2(node a, node b) {
return a.time < b.time;
}
int main() {
int n, k, maxtime = -, tempindex = ;
scanf("%d%d\n", &n, &k);
vector<node> record(n), car;//将其确定大小,后序可以使用下标访问。
for(int i = ; i < n; i++) {
char temp[];
int h, m, s;
//输入这个字符串的时候不用再&符号了!
scanf("%s %d:%d:%d %s\n", record[i].id, &h, &m, &s, temp);
int temptime = h * + m * + s;
record[i].time = temptime;
record[i].flag = strcmp(temp, "in") == ? : -;
}
sort(record.begin(), record.end(), cmp1);
map<string, int> mapp;//id映射到时间。
for(int i = ; i < n - ; i++) {
if(strcmp(record[i].id, record[i+].id) == && record[i].flag == && record[i+].flag == -) {
car.push_back(record[i]);
car.push_back(record[i+]);
mapp[record[i].id] += (record[i+].time - record[i].time);
if(maxtime < mapp[record[i].id]) {
maxtime = mapp[record[i].id];
}
}
}
sort(car.begin(), car.end(), cmp2);//再对car进行排序。
vector<int> cnt(n);
for(int i = ; i < car.size(); i++) {
if(i == )
cnt[i] += car[i].flag;
else
cnt[i] = cnt[i - ] + car[i].flag;
}
for(int i = ; i < k; i++) {
int h, m, s;
scanf("%d:%d:%d", &h, &m, &s);
int temptime = h * + m * + s;
int j;
for(j = tempindex; j < car.size(); j++) {
if(car[j].time > temptime) {//如果时间已经大于了当前,那么就不算。
printf("%d\n", cnt[j - ]);
break;
} else if(j == car.size() - ) {
printf("%d\n", cnt[j]);
}
}
tempindex = j;
}
for(map<string, int>::iterator it = mapp.begin(); it != mapp.end(); it++) {
if(it->second == maxtime)
printf("%s ", it->first.c_str());
}
printf("%02d:%02d:%02d", maxtime / , (maxtime % ) / , maxtime % );
return ;
}
//这个cnt数组思路很好,学习了,需要复习啊,待会回想一下。
PAT 1095 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 分 ...
- 1095 Cars on Campus——PAT甲级真题
1095 Cars on Campus Zhejiang University has 6 campuses and a lot of gates. From each gate we can col ...
- pat 甲级 Cars on Campus (30)
Cars on Campus (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard 题目描述 Zhejiang University ...
- PAT (Advanced Level) Practise - 1095. Cars on Campus (30)
http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...
- PAT (Advanced Level) 1095. Cars on Campus (30)
模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...
- PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)
题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...
- 【PAT甲级】1095 Cars on Campus (30 分)
题意:输入两个正整数N和K(N<=1e4,K<=8e4),接着输入N行数据每行包括三个字符串表示车牌号,当前时间,进入或离开的状态.接着输入K次询问,输出当下停留在学校里的车辆数量.最后一 ...
- 1095. Cars on Campus (30)
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...
随机推荐
- PHP代码审计学习之命令执行漏洞挖掘及防御
[1]可能存在命令执行漏洞的函数: 00x1:常用的命令执行函数:exec.system.shell_exec.passthru 00x2:常用的函数处理函数:call_user_func.call_ ...
- Github上fork项目后与原项目保持同步
**步骤** 假设来源为 `https://github.com/_original/_project.git` fork 项目为 `https://github.com/_your/_projec ...
- Supervisord常见用法和介绍
Supervisord是用Python实现的一款非常实用的进程管理工具.supervisord会帮你把管理的应用程序转成daemon程序,而且可以方便的通过命令开启.关闭.重启等操作,而且它管理的进程 ...
- 在一个验证form的实例中扩展jQuery.validate
需求很简单,直接上图: 要验证表单上的3个input输入框的格式,要求如下: 主关键词情形1: 浙江 杭州 温州 主关键词情形2: 浙江|江苏|上海,但是不能用 空格和 | 混合用,也就是情形1和2不 ...
- 加速I/O的基本规则
作为这个讨论的开始,这里有几个如何加速I/O的基本规则: 1. 避免访问磁盘 2. 避免访问底层的操作系统 3. 避免方法调用 4. 避免个别的处理字节和字符 很明显这些规则不能在所有的问题上避免,因 ...
- 【tyvj】P2065 「Poetize10」封印一击(贪心+线段树/差分)
http://new.tyvj.cn/p/2065 我就不说我很sb的用线段树来维护值...... 本机自测的时候想了老半天没想出怎么维护点在所有区间被多少区间包含的方法.最后一小时才想出来线段树(果 ...
- Anaconda2+Theano 安装过程中的所有的坑。。。
写在前面的废话 上次搞theano安装还是一年多以前..anaconda才出到1.4,当时的AnacondaCE,直接安装完基本上theano啥的都一套成功.. 今天换了个电脑,重装anaconda, ...
- Learning to Compare: Relation Network 源码调试
CVPR 2018 的一篇少样本学习论文 Learning to Compare: Relation Network for Few-Shot Learning 源码地址:https://github ...
- py-faster-rcnn 训练自己的数据
转载:http://blog.csdn.net/sinat_30071459/article/details/51332084 Faster-RCNN+ZF用自己的数据集训练模型(Python版本) ...
- Duilib教程-控件练习
一.控件消息的响应. 在HelloDuilib例子中,程序不能退出,在这里,我将添加一个关闭按钮,当点击它时,调用PostQuitMessage进行退出. 首先在界面的右上角添加一个关闭按钮,并取名为 ...