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.

Input Specification:

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.

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

给出一些车辆进出的记录,要求给定一时间判断此时有多少车是in的状态(还没有out),末尾输出停留时间最多的车的编号(字母序),以及停留时间。
首先要排序才知道先后顺序,然后去除五匹配的in和out记录,把有效的记录单独存一下,并计算每一时刻处于in状态的车数,查询的时候二分。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
///所有的时间点都是在一天当中24小时制 不存在同一时刻一辆车又进又出 至少有完整一辆车的进出记录
///没有对应out的in直接忽略 反过来也是
struct park {
char pno[],state[];///车牌号 进出状态
int t,pnum;///对应时间 对应时间校内车辆
}c[],up[];
int n,k,num,ans,pnum,last[],nn,del[];
pair<string,int> car[];
bool cmp(park a,park b) {
return a.t < b.t;
}
bool cmp1(pair<string,int> a,pair<string,int> b) {
if(a.second == b.second)return a.first < b.first;
return a.second > b.second;
}
int gettime(int h,int m,int s) {
return h * + m * + s;
}
int getnum(int t) {///二分求第一个大于当前时间的位置
if(t < up[].t || t >= up[nn - ].t)return ;///始末位置车数都为0 因为去除了不匹配记录 所以不存在校内的车一直没有out的情况
int l = ,r = nn - ,mid;
while(l < r) {
mid = (l + r) / ;
if(up[mid].t > t)r = mid;
else l = mid + ;
}
return up[l - ].pnum;///求得的是时间>t的位置 所以需要-1
}
int main() {
int h,m,s,t;
char pno[],state[];
map<string,int> q;
scanf("%d%d",&n,&k);
for(int i = ;i < n;i ++) {
scanf("%s %d:%d:%d %s",c[i].pno,&h,&m,&s,c[i].state);
c[i].t = gettime(h,m,s);
}
sort(c,c + n,cmp);///先按时间排序
for(int i = n - ;i >= ;i --) {///去除不匹配的
if(!q[c[i].pno]) {///第一次出现的车号 则给他分配一个位置
q[c[i].pno] = ++ num;
last[num] = -;
car[num] = pair<string,int>(c[i].pno,);
}
int d = last[q[c[i].pno]];
if(c[i].state[] == 'o' && d != - && c[d].state[] == 'o') {
del[d] = ;///表示无效的记录
}
else if(c[i].state[] == 'i' && (d == - || c[d].state[] == 'i')) {
del[i] = ;
}
last[q[c[i].pno]] = i;
}
for(int i = ;i < n;i ++) {
if(del[i])continue;
int d = last[q[c[i].pno]];
if(c[i].state[] == 'o' && c[d].state[] == 'o')continue;
up[nn ++] = c[i];///记录正确的记录
last[q[c[i].pno]] = i;
}
for(int i = ;i < nn;i ++) {
if(up[i].state[] == 'i') {///pnum记录校内停留车辆数目
pnum ++;///当前时间 校内车辆数加1
car[q[up[i].pno]].second -= up[i].t;///记录该车停留时间 out - in 所以这里是减去
}
else {
pnum --;///当前时间校内车辆数-1
car[q[up[i].pno]].second += up[i].t;///加上out
}
up[i].pnum = pnum;///当前时间校内车辆数更新
}
sort(car + ,car + num + ,cmp1);
for(int i = ;i < k;i ++) {
scanf("%d:%d:%d",&h,&m,&s);
t = gettime(h,m,s);
printf("%d\n",getnum(t));
}
ans = car[].second;
printf("%s",car[].first.c_str());
int i = ;
while(car[i].second == ans) {
printf(" %s",car[i ++].first.c_str());
}
printf(" %02d:%02d:%02d",ans / ,ans / % ,ans % );
}

1095 Cars on Campus (30)(30 分)的更多相关文章

  1. 1095 Cars on Campus(30 分

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

  2. 【刷题-PAT】A1095 Cars on Campus (30 分)

    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

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

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

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

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

  6. PAT甲级1095. Cars on Campus

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

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

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

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

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

    Xenomai 3 的rtdm驱动更像一般的Linux驱动,named device会在/dev/rtdm/xxx创建一个设备文件.而用户空间使用时,写得来也和Linux的一般char设备相似,ope ...

  2. 【WPF学习笔记】[转]周银辉之WPF中的动画 && 晓风影天之wpf动画——new PropertyPath属性链

    (一)WPF中的动画 动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互.这里我们讨论一下故事板. 在WPF中我们采用Storyboard(故事板)的方式 ...

  3. MySQL数据库的常见操作(七)

    MySQL数据库的常见操作 1.创建数据库 2.创建重名的数据库以及如何查看警告信息 3.设置数据库的编码方式(默认为utf8) 4.修改和查看数据库的编码方式 5.删除数据库 6.6.删除已经删除了 ...

  4. 【转】iOS安全之RSA加密/生成公钥、秘钥 pem文件

    在iOS中使用RSA加密解密,需要用到.der和.p12后缀格式的文件,其中.der格式的文件存放的是公钥(Public key)用于加密,.p12格式的文件存放的是私钥(Private key)用于 ...

  5. 微信小程序设置控件权重

    项目中最常用的两种布局方式,水平布局和垂直布局,在微信小程序中实现起来也比较简单.       1.横向水平布局:         实现水平布局,需要四个view容器组件,其中一个是父容器.如下: & ...

  6. c++动态绑定的技术实现

    1 什么是动态绑定 有一个基类,两个派生类,基类有一个virtual函数,两个派生类都覆盖了这个虚函数.现在有一个基类的指针或者引用,当该基类指针或者引用指向不同的派生类对象时,调用该虚函数,那么最终 ...

  7. java array

    1 array变量 Type[] array_virable_name; 2 array对象 2.1 new Type[] array_virable_name = new Type[NUM]; 2. ...

  8. 【题解】DZY Loves Chinese

    [题解]DZY Loves Chinese II 不吐槽这题面了... 考虑如何维护图的连通性,如果把图的变成一颗的\(dfs\)生成树,那么如果把一个节点的父边和他接下来所有的返祖边删除,那么我们就 ...

  9. 【足迹C++primer】35、特定容器算法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/33732681 特定容器算法 lst.me ...

  10. 开源Flex Air版免费激情美女视频聊天室,免费网络远程视频会议系统((Flex,Fms3联合打造))

    开源Flex Air版免费激情美女视频聊天室,免费网络远程视频会议系统((Flex,Fms3联合打造))   Flex,Fms3系列文章导航 Flex,Fms3相关文章索引 本篇是视频聊天,会议开发实 ...