PAT甲级1095. Cars on Campus

题意:

浙江大学有6个校区和很多门。从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码。现在有了所有的信息,你应该在任何特定的时间点告诉在校园停车的数量,

在一天结束时,发现停车时间最长的汽车。

输入规格:

每个输入文件包含一个测试用例。每个案例从两个正整数N(<= 10000)开始,记录数,K(<= 80000)查询次数。然后按N行,每个都以格式记录

plate_number hh:mm:ss status

其中plate_number是7个英文大写字母或1位数字的字符串; hh:mm:ss表示一天中的时间点:分钟:秒,最早的时间为00:00:00,最晚为23:59:59;并且状态是进出。

请注意,所有时间都将在一天之内。

每个“in”记录与同一辆车的时间顺序的下一个记录配对,只要它是一个“出”记录。与“out”记录不配对的任何“in”记录都将被忽略,“out”记录也不与“in”记录配对。保证在输入中至少有一辆汽车配对良好,

没有车在同一时刻都在“进”和“出”。使用24小时制记录时间。

然后,K行的查询跟随,每个给出一个格式为hh:mm:ss的时间点。注意:查询按照时间顺序给出。

输出规格:

对于每个查询,输出一行在校园内停车总数。

输出的最后一行应该是给出停留时间最长的车辆的牌号和相应的时间长度。如果这样的车不是唯一的,那么按字母顺序输出所有的牌号,并以空格分隔。

思路:

模拟题还是恶心人呀。

本题中查询数据量特别大,所以查询一定要处理好。 由于是升序,所以可用用一个数组count_time[24 * 60 * 60]模拟,找出valid的数据在start++;在end--;就ok了

ac代码:

C++

// pat1095.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set> using namespace std; int n, k; int all_time_state[86401]; struct records
{
//char plate[8];
string plate;
int state; //1 : in 0 : out
int time;
}; struct car
{
string plate;
//vector<pair<int, int> > time;
int state = 0;
int state_time = 0;
int total = 0;
}; unordered_map<string, car> mymap; int time_to_int(char* temptime)
{
int hour, minute, second;
hour = (temptime[0] - '0') * 10 + (temptime[1] - '0');
minute = (temptime[3] - '0') * 10 + (temptime[4] - '0');
second = (temptime[6] - '0') * 10 + (temptime[7] - '0');
return hour * 3600 + minute * 60 + second;
} bool recmp(records& a, records& b)
{
return a.time < b.time;
} bool staycmp(string& a, string& b)
{
return a < b;
} void wash_data(vector<records>& re) //去除噪声数据
{
sort(re.begin(), re.end(), recmp);
vector<records> res;
for (int i = 0; i < n; i++)
{
if (mymap.find(re[i].plate) == mymap.end())
{
mymap[re[i].plate].state = re[i].state;
mymap[re[i].plate].state_time = re[i].time;
}
else
{
if (mymap[re[i].plate].state == 1 && re[i].state == 0)
{ mymap[re[i].plate].plate = re[i].plate;
all_time_state[mymap[re[i].plate].state_time]++;
all_time_state[re[i].time]--;
//for (int j = mymap[re[i].plate].state_time; j < re[i].time; j++)
//{
// all_time_state[j]++;
//}
//mymap[re[i].plate].time.push_back(make_pair(mymap[re[i].plate].state_time, re[i].time));
mymap[re[i].plate].total += re[i].time - mymap[re[i].plate].state_time; }
mymap[re[i].plate].state = re[i].state;
mymap[re[i].plate].state_time = re[i].time;
}
}
} int main()
{
scanf("%d %d", &n, &k); char tempplate[8], temptime[9], tempstate[4];
vector<records> re;
for (int i = 0; i < n; i++) //input records
{
records r;
scanf("%s %s %s", &tempplate, &temptime, &tempstate);
r.plate = string(tempplate);
r.time = time_to_int(temptime);
if (tempstate[0] == 'i') r.state = 1;
else r.state = 0;
re.push_back(r);
} //wash data
memset(all_time_state, 0, sizeof(all_time_state));
wash_data(re); //handle
int query_time,last_query = 0, carcnt = 0;
for (int i = 0; i < k; i++)
{
scanf("%s", temptime);
query_time = time_to_int(temptime);
while (last_query <= query_time) carcnt += all_time_state[last_query++];
printf("%d\n", carcnt);
} vector<string> longstay;
int long_stay_time = -1;
for (auto it = mymap.begin(); it != mymap.end(); it++)
{
if (it->second.total > long_stay_time)
{
long_stay_time = it->second.total;
longstay.clear();
longstay.push_back(it->first);
}
else if(it->second.total == long_stay_time)
{
longstay.push_back(it->first);
}
} sort(longstay.begin(), longstay.end(), staycmp);
for (int i = 0; i < longstay.size(); i++)
{
cout << longstay[i] << " ";
}
printf("%02d:%02d:%02d\n", long_stay_time / 3600, (long_stay_time % 3600) / 60, long_stay_time % 60);
return 0;
}

PAT甲级1095. Cars on Campus的更多相关文章

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

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

  2. PAT甲级——A1095 Cars on Campus

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

  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 1095 Cars on Campus

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

  5. 【刷题-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 ...

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

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

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

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

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

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

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

随机推荐

  1. 【zTree】zTree的3.5.26静态树与动态树(实用)

    1.静态树: 目录结构:(css与js为下载的原文件夹)

  2. php菜刀分析学习

    这里以eval为例 我们知道, php中的eval能把字符串当代码执行: eval('phpcode'); 注意, 这里的代码要有分号结尾, 我们测试: 我们创建一个最简单的SHELL: <?p ...

  3. svn add --no-ignore

    提交新代码时:svn add --no-ignore  /dir   不加的话可能会漏提交某些依赖或文件. Svn st -q --no-ignore. 提交时不需要加

  4. P1084 疫情控制

    Solution 二分答案, 尽量往上跳, 不能跳到根节点. 仍然能跳的拿出来.看剩下的点没有覆盖哪个? 贪心的分配一下. Code 70 #include<iostream> #incl ...

  5. 搜索关键词智能提示suggestion

    转载自:stormbjm的专栏 题目详情:百度搜索框中,输入“北京”,搜索框下面会以北京为前缀,展示“北京爱情故事”.“北京公交”.“北京医院”等等搜索词,输入“结构之”,会提示“结构之法”,“结构之 ...

  6. 洛谷P2676 超级书架 题解

    题目传送门 题目一看就是贪心.C++福利来了:sort. 基本思路就是:要使奶牛最少那么肯定高的奶牛先啦. 直接排序一遍(从高到矮)然后while,搞定! #include<bits/stdc+ ...

  7. Weblogic常用监控指标以及监控工具小结

    https://blog.csdn.net/hualusiyu/article/details/39583549

  8. RabbitMQ指南之二:工作队列(Work Queues)

    在上一章的指南中,我们写了一个命名队列:生产者往该命名队列发送消息.消费从从该命名队列中消费消息.在本章中,我们将创建一个工作队列,用于在多个工作者之间分配耗时的任务.工作队列(即任务队列)的主要思想 ...

  9. 自动化测试框架Cucumber和Robot Framework的实战对比

    自动化测试框架Cucumber和RobotFramework的实战对比 一.摘要 自动化测试可以快速自动完成大量测试用例,节约巨大的人工测试成本:同时它需要拥有专业开发技能的人才能完成开发,且需要大量 ...

  10. 超简教程:Xgboost在Window上的安装(免编译)

    Xboost在windows安装需要自己编译,编译的过程比较麻烦,而且需要复杂的软件环境.为了免去编译,我这里把编译好的文件上传到网盘供大家下载安装.有了编译好的文件,xgboost的安装变得超级简单 ...