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 ...
随机推荐
- iOS UITableViewHeaderFooterView设置背景色为透明
给UITableViewHeaderFooterView设置背景色分2种情况 1.tableView在group模式下,UITableViewHeaderFooterView的背景色默认是透明的,此时 ...
- linux上FTP服务器搭建
一.启动vsftpd服务 1. 启动VSFTP服务器 A. cenos下运行:yum install vsftpd B. 登录Linux主机后,运行命令:"service vsftpd st ...
- gitlab手残点错关闭注册No authentication methods configured on login page
Gitlab - 如何解決 "No authentication methods configured on login page" ? (gitlab version : 8.1 ...
- c# 实现遍历 DataTable 和DataSet (简单的方式)
今天 做一个小程序 ,遇到了这样一个 问题就是 怎样简单的 遍历一个 DataTable 一. DataTable table= DBhelper.GetDataTable(str);foreach( ...
- Git—怎样Windows操作系统中安装Git
介绍一下怎样在Windows操作系统中安装Git: 一.下载Git安装压缩文件:http://download.csdn.net/detail/wangshuxuncom/8035045 二.解压该压 ...
- CI和CD的意思
openstack中CI和CD的意思: 持续集成(CI)和持续交付(CD)
- websphere web.xml
解决WAS更新web.xml文件不生效的问题(web_merged.xml是罪魁祸首) 问题原因分析 近日碰到更新web.xml文件到WAS服务器(WebSphere Application Se ...
- 百度订单Api注意事项
背景介绍: 申请的百度地图API,采用javascript sdk方式 页面引用 问题1:更换域名导致定位插件不能用 需要修改百度地图-应用中的白名单设置,按照规则添加新的域名 问题2:http与ht ...
- iOS 下载
#import "ViewController.h" @interface ViewController () @property (strong, nonatomic) NSMu ...
- form.submit 方法 并不会触发 form.onsubmit 事件
做表单的时候发现一个奇怪的地方,总结下: form.submit 方法 并不会触发 form.onsubmit 事件,看代码: <body> <div class="con ...