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. python 练习题1--打印三位不重复数字

    题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源代码 ...

  2. linux学习笔记2---命令cd

    Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的.cd命令比较简单,但是有一些技巧还是值得学习的. 所以,学习Linux 常用命令, ...

  3. IT 服务管理工具 iTop

    iTop,作为全面支持ITIL流程的一款ITSM工具,具有强大的ITSM功能,开源免费.简单易用. iTop,即IT运营门户(IT Operation Portal),是一个开源web应用程序,用于I ...

  4. hdu6058 Kanade's sum 区间第k大

    /** 题目:Kanade's sum 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6058 题意:给定[1,n]的排列,定义f(l,r,k)表示区间[l ...

  5. 解决微信小程序中Date.parse()获取时间戳IOS不兼容的问题(IOS为NaN的问题)

    前端同事在做微信小程序时发现IOS获取的时间戳为空的问题,后来通过跟踪发现,原来是因为IOS系统不支持2017-01-01格式的时间导致的, var mydata = '2017-01-01 11:0 ...

  6. 【Selenium】之谷歌、IE、火狐浏览器各个版本的浏览器驱动下载地址

    地址:chromedriver官网下载地址: http://chromedriver.storage.googleapis.com/index.html(失效了) http://npm.taobao. ...

  7. nodejs 聊天室简单实现

    前言 博客园的样式真心不会用啊,看着大大们的博客各种好看,心里无奈啊,只能慢慢摸索了. 最近的项目nodejs+wcf+app,app直接从wcf服务获取数据,nodejs作为单独的服务器为app提供 ...

  8. PAT002 Reversing Linked List

    题目: Given a constant K and a singly linked list L, you are supposed to reverse the links of every K ...

  9. HBase之HFile解析

    Sumary: Protobuf BinarySearch 本篇主要讲HFileV2的相关内容,包括HFile的构成.解析及怎么样从HFile中快速找到相关的KeyValue.基于Hbase 0.98 ...

  10. Linux初学者学习资料

    鸟哥的Linux私房菜 http://vbird.dic.ksu.edu.tw/linux_basic/linux_basic.php