Cars on Campus (30)

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard 

题目描述

Zhejiang University has 6 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.

输入描述:

Each input file contains one test case.  Each case starts with two positive integers N (<= 10000), the number of records, and K (<= 80000) 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 ascending order of the times.

输出描述:

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.

输入例子:

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

输出例子:

1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09
题意:一天当中不同时间段都会有车进或出停车场,给定一个时间点,判断这个时间点上有多少车停留在停车场上。并且最后输出在停车场上逗留时间最长的车的号码以及逗留时间。
思路:要注意每辆车每天可能会多次进出停车场,并且每一个进场的记录必须与时间离它最近的一个出场纪录配对,匹配不到出场纪录则这个进场记录无效,匹配不到进场记录的出场纪录也无效。
直接模拟,不过发现有一个样例有时过,有时超时,有点卡时,后来看了别人的题解,发现可以用树状数组优化,以1秒为以1单位,所有时间转化成秒。这样若有一个记录,车辆进出场时间段为[l,r],
那么树状数组维护的[l,r]区间每个位置都从0变成1即可。查询一辆车某个时间点是否在场,只需判断这个时间点是否为1。最后找逗留时间最长的车辆,对树状数组取和即可知道一辆车逗留时长。有时间时可以再用树状数组做做
模拟代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
#define N_MAX 10000+5
#define INF 0x3f3f3f3f
typedef long long ll;
int n, k;
struct Time {
int value;
bool is_in=;
Time() {}
Time(int value,bool is_in):value(value),is_in(is_in) {}
bool operator < (const Time & b) const{
return value < b.value;
}
}; struct Car {
string id;
set<Time>time;
vector<pair<int,int> >vec;//存储一对进出的信息
int sum_time=;
}car[N_MAX];
map<string, int>car_index;
int tran(int h,int min,int s) {
return h * + min * + s;
}
vector<int> recover(int x) {
vector<int>vec; vec.resize();
vec[]=(x / );
x %= ;
vec[]=(x / );
x %= ;
vec[]=x;
return vec;
}
int main() {
while (scanf("%d%d",&n,&k)!=EOF) {
int num = ;
for (int i = ; i < n;i++) {
int hour, min, sec, value; string id; char status[],Id[];
scanf("%s",Id);
id = Id;
scanf("%d:%d:%d",&hour,&min,&sec);value = tran(hour, min, sec);
scanf("%s",status);
if (status[] == 'i') {
if (car_index[id] == ) { car_index[id] = num++; car[car_index[id]].id = id; }//添加新车记录
car[car_index[id]].time.insert(Time(value,));
}
else {
if (car_index[id] == ) { car_index[id] = num++; car[car_index[id]].id = id; }//添加新车记录
car[car_index[id]].time.insert(Time(value,));
}
} for (int i = ; i < num;i++) {//筛选正确信息
bool flag = ;//判断是否有需要匹配的时间点
Time need_match;
for (set<Time>::iterator it = car[i].time.begin(); it != car[i].time.end();it++) {
Time time = *it;
if (!flag&&time.is_in) { flag = ; need_match = time; }
else if (flag&&time.is_in) need_match = time;
else if (flag&&time.is_in == ) {
car[i].vec.push_back(make_pair(need_match.value, time.value));
car[i].sum_time += time.value - need_match.value;
flag = ;
}
}
} while (k--) {
int hour, min, sec, time,cnt=;
scanf("%d:%d:%d", &hour, &min, &sec);
time = tran(hour, min, sec);
for (int i = ; i < num;i++) {
for (int j = ; j < car[i].vec.size();j++) {
if (time >= car[i].vec[j].first&&time < car[i].vec[j].second) {
cnt++; break;
}
}
}
printf("%d\n",cnt);
}
set<string>S;
int max_time=;
for (int i = ; i < num;i++) {
if (car[i].sum_time > max_time) {
max_time = car[i].sum_time;
S.clear();
S.insert(car[i].id);
}
else if (car[i].sum_time == max_time) {
S.insert(car[i].id);
}
}
for (set<string>::iterator it = S.begin(); it != S.end();it++) {
printf("%s ",(*it).c_str());
}
vector<int>rec = recover(max_time);
printf("%02d:%02d:%02d\n",rec[],rec[],rec[]);
}
return ;
}

pat 甲级 Cars on Campus (30)的更多相关文章

  1. A1095 Cars on Campus (30)(30 分)

    A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...

  2. PAT 1095 Cars on Campus

    1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...

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

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

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

  5. PAT A1095 Cars on Campus (30 分)——排序,时序,从头遍历会超时

    Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...

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

  9. 1095 Cars on Campus (30)(30 分)

    Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...

随机推荐

  1. 牛客小白月赛5 A 无关(relationship) 【容斥原理】【数据范围处理】

    题目链接:https://www.nowcoder.com/acm/contest/135/A 题目描述 若一个集合A内所有的元素都不是正整数N的因数,则称N与集合A无关.   给出一个含有k个元素的 ...

  2. LAMP 搭建练习

    目录 LAMP 搭建 1:CentOS 7, lamp (module): http + php + phpMyAdmin + wordpress 192.168.1.7 配置虚拟主机 xcache ...

  3. win10.net 安装出问题0x800F70422

    因为安装ooracle数据库的时候需要用到.net但安装的时候出了0x800F70422, 随后就去网上查了下这个错出现的原因,发现是我之前把Windows自带的更新给禁用了 只要把它再开启就行了.

  4. php面向对象(2)构造和析构函数

    一.构造方法 构造方法是类中一个“特殊”的方法,作用是在实例化一个对象的同时,给该对象的属性赋值,使之创建完成的时就具有其本身的特有属性 该方法固定格式:[访问修饰符] function _const ...

  5. uncompressing linux .................................................后没反应解决办法

    编译kernel是的no machine record defined 错误,网上有一些解法,其实都是错误的,以讹传讹.不打算自己写,找到一篇还算靠谱的,转摘一下. 其根本原因是没有在 __proc_ ...

  6. Django框架基础知识01-配置环境

    Django框架 Django是个怎样的东西呢? Web应用框架----Django  http服务器:用来接受用户请求,并将请求转发给web应用框架进行处理. Web应用框架处理完以后再发送给htt ...

  7. Python的集合与字典练习

    集合与字典练习 question1 问题描述:有一个列表,其中包括 10 个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表 ...

  8. (转)Xcode6中自动布局autolayout和sizeclass的使用

    Xcode6中自动布局autolayout和sizeclass的使用   一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的, ...

  9. 添加SQL字段

    通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数增加字段: alter table [表名] add 字段名 smallin ...

  10. Linux5个数据段

        进程(执行的程序)会占用一定数量的内存,它或是用来存放从磁盘载入的程序代码,或是存放取自用户输入的数据等等.不过进程对这些内存的管理方式因内存用途不一而不尽相同,有些内存是事先静态分配和统一回 ...