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. iOS学习笔记10 - Bundle和Info.plist

    经常会有需要从应用中搜索并读取一个文件或图片.这时候就会用到如下的语句: NSString *path = [[NSBundle mainBundle] pathForResource:@" ...

  2. java 线程之间的协作 wait()与notifyAll()

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  3. 封装常用的selenium方法

    package com.yk.userlive.base; import java.net.MalformedURLException;import java.net.URL;import java. ...

  4. 腾讯课堂1:使用Jmeter内置的录制功能进行录制

    1.设置http代理服务器 打开火狐——点击选项——高级——网络——设置  设置完成点击确定 2.查看端口是否被占用的命令 netstat -ano 3.排除模式 .*\.gif .*\.css .* ...

  5. 简单说明什么是递归?什么情况会使用?并使用java实现一个简单的递归程序。

    解答: 1)递归做为一种算法在程序设计语言中广泛应用.是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象. 2)递归算法一般用于解决三类问题: a.数据的定义是按递归定义的.(Fib ...

  6. Apollo 刨析:简介

      Apollo是配置在IIS服务器上的一个Web站点,它使用了.NET4.0和ASP.Net的技术. 代码是C#编写的.是基于ASP.NET MVC3的Web开发框架上编写的一个应用. 它使用到了N ...

  7. 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3016 之前yy了一个贪心,,,但是错了,,就是枚举前后对应的字符(前面第i个和后面第i个)然后相同答 ...

  8. 【BZOJ】3401: [Usaco2009 Mar]Look Up 仰望(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3401 还能更裸一些吗.. 维护一个递减的单调栈 #include <cstdio> #i ...

  9. string与wstring互转

    string与wstring互转  C++ Code  123456789101112131415161718192021222324252627282930313233343536373839404 ...

  10. SQL Server 数据库分离与附加(图文教程)

    from:http://www.jb51.net/article/36624.htm 一.概述 SQL Server提供了“分离/附加”数据库.“备份/还原”数据库.复制数据库等多种数据库的备份和恢复 ...