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 (≤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 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的更多相关文章

  1. PAT甲级1095. Cars on Campus

    PAT甲级1095. Cars on Campus 题意: 浙江大学有6个校区和很多门.从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码.现在有了所有的信息,你应该在任何特定的时间点告诉在 ...

  2. PAT甲级——1095 Cars on Campus (排序、映射、字符串操作、题意理解)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93135047 1095 Cars on Campus (30 分 ...

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

  4. pat 甲级 Cars on Campus (30)

    Cars on Campus (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard  题目描述 Zhejiang University ...

  5. 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 ...

  6. PAT (Advanced Level) 1095. Cars on Campus (30)

    模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...

  7. PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)

    题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...

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

    题意:输入两个正整数N和K(N<=1e4,K<=8e4),接着输入N行数据每行包括三个字符串表示车牌号,当前时间,进入或离开的状态.接着输入K次询问,输出当下停留在学校里的车辆数量.最后一 ...

  9. 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 ...

随机推荐

  1. try...except...后中断程序继续运行

    今天遇到一个问题,本来是以前已经了解过的,但是忘了,现在又想起来了,还是记一下 try...except...当except抓到异常后程序会继续运行 但是个人感觉抓到异常后都是终止程序的吧...可能也 ...

  2. 公共查询类criteria

    package cn.edu.hbcf.common.vo; import java.math.BigDecimal; import java.sql.Timestamp; import java.u ...

  3. ffmpeg 和 x264的参数对照

    ffmpeg 和 x264的参数对照   x264 ffmpeg 说明 命令行 字段 命令行 字段 qp qp_constant cqp cqp 固定量化因子.取值范围0到51. 经常取值在20-40 ...

  4. jQuery实现浮动层跟随页面滚动效果

      helloweba.com Author:月光光 Time:2010-11-29 09:02 Tag: jquery  滚动 在本文中,我将介绍一个可以跟随页面滚动的层效果,当用户滚动鼠标滚轮或者 ...

  5. 02 Java图形化界面设计——中间容器(Jpanel)

    上一篇讲解了Jframe顶层容器,例子中生成了一个空的窗体,在实际编程过程中,一般很少将文本框.按钮等组件直接放在顶层容器中进行布局,大多数时候是通过布局管理器结合中间容器对组件进行布局设置. 1.  ...

  6. 请说明meta标签的作用。

    请说明meta标签的作用. 解答: meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多 ...

  7. FreeRTOS系列第17篇---FreeRTOS队列

    本文介绍队列的基本知识,具体源代码分析见<FreeRTOS高级篇5---FreeRTOS队列分析> 1.FreeRTOS队列 队列是基本的任务间通讯方式.能够在任务与任务间.中断和任务间传 ...

  8. 基于字典SR各种方法【稀疏编码多种方法】

    基于字典的图像超分辨率实现 - CSDN博客 http://blog.csdn.net/u011630458/article/details/65635155 简介 这段时间在看基于字典的单帧图像超分 ...

  9. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

  10. 第一只python爬虫

    import urllib.request response = urllib.request.urlopen("http://www.baidu.com") html = res ...