http://www.patest.cn/contests/pat-a-practise/1095

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

这道题是2015考研机试前的那个PAT的D题 http://www.patest.cn/contests/pat-a-101-125-1-2015-03-14

那次PAT据说比机试简单。。。于是很多高分大神申请了免机试。。。性价比超高。。。。

我表示很忧伤。。。虽然我对自己机试成绩还算满意,但是还是有点感慨。。。如果参加这次PAT 说不定分数更好。。。嗯,做梦的感觉好好好哦。。。

T_T  并不能轻易的承认 这貌似是我在pat上扯过的最长的代码了。。。不过好消息是A级我才做三分之一不到 哈哈哈哈 说不定有更长的

 #include<cstdio>
#include<cstring>
struct carrecord
{
long long carid;
int second;
int status; // 1-in 0-out
}records[];
int recordsnum=,queriesnum=,time[][]={}; long long str2int(char *str)//一个大于 (26字母+10数字)的数值即可 用于保持比较时有效的字典序列
{
long long num=,istr=;
while(str[istr]) num=num*+((''<=str[istr]&&str[istr]<='')?str[istr]-'':str[istr]-'A'+),istr++;
return num;
} void outlook(long long num)//一条记录输出
{
char car[]="";
int istr=,temp=;
car[]='\0';
while(istr>=)//int2str 恢复字符串名字
{
temp=num%;
if(temp>=) car[istr]=temp+'A'-;
else car[istr]=temp+'';
num/=,istr--;
}
printf("%s ",car);
} //基于汽车牌号(以转为对应序列的数值)和进出时间的快排
void QS(int low,int high,const int iqs) //1-carid 0-time
{
int l=low,h=high,second=records[l].second,status=records[l].status;
long long carid=records[l].carid; while(l<h)
{
if(iqs) while( l<h && (carid<records[h].carid ||(carid==records[h].carid && second<=records[h].second))) h--;
else while( l<h && (second<records[h].second ||( carid<=records[h].carid && second==records[h].second ))) h--;
if(l<h)
{
records[l].carid=records[h].carid;
records[l].second=records[h].second;
records[l].status=records[h].status; records[h].carid=carid;
records[h].second=second;
records[h].status=status;
} if(iqs) while( l<h && (carid>records[l].carid||(carid==records[l].carid&& second>=records[l].second))) l++;
else while( l<h && (second>records[l].second ||(carid>=records[l].carid&& second==records[l].second))) l++;
if(l<h)
{
records[h].carid=records[l].carid;
records[h].second=records[l].second;
records[h].status=records[l].status; records[l].carid=carid;
records[l].second=second;
records[l].status=status;
}
}
if(low+<l) QS(low,l-,iqs);
if(h+<high) QS(h+,high,iqs);
} int clean(int len,int in,int out)
{
int l=len,h=in;
records[l].carid=records[h].carid;
records[l].second=records[h].second;
records[l].status=records[h].status; l=l+,h=out;
records[l].carid=records[h].carid;
records[l].second=records[h].second;
records[l].status=records[h].status; return len+;
} int main()
{
scanf("%d%d",&recordsnum,&queriesnum); char carname[],status[];
int hh,mm,ss;
for(int i=;i<=recordsnum;i++)//从1开始是为了方便下面记录配对时直接移动即可 而不需temp
{
scanf("%s %d:%d:%d %s",carname,&hh,&mm,&ss,status);
records[i].carid=str2int(carname);// 比较、移动、复制等,操作简单,节省时间
records[i].second=ss+*(mm+*hh);
records[i].status=(==strcmp(status,"in")?:);
}
QS(,recordsnum,);// 基于汽车牌号(以转为对应序列的数值)的QS int num=,len=,flagin=-,flagout=-;
long long carid=;
while(num<=recordsnum)// Each "in" record is paired with the chronologically next record for the same car provided it is an "out" record
{
flagin=-,flagout=-,carid=records[num].carid;
while(carid==records[num].carid && num<=recordsnum) // Any "in" records that are not paired with an "out" record are ignored, as are "out" records not paired with an "in" record.
{
if(==records[num].status) flagin=num,flagout=-;
else if(-==flagin) flagin=-,flagout=-;
else len=clean(len,flagin,num),flagin=-,flagout=-;
num++;
}
}
recordsnum=len;
QS(,recordsnum-,); num=;
int istr=,carnum=,notfirst=;
if(recordsnum) notfirst=;
while(num<queriesnum) //For each query, output in a line the total number of cars parking on campus
{
scanf("%d:%d:%d",&hh,&mm,&ss);
len=ss+*(mm+*hh);
while(len>=records[istr].second && istr<recordsnum)
{
if(records[istr].status) carnum++;
else carnum--;
istr++;
}
if(num<queriesnum-notfirst) printf("%d\n",carnum);
else printf("%d",carnum);
num++;
} QS(,recordsnum-,);
num=,istr=,flagout=;
while(num<recordsnum) //the longest time period parked for
{
istr=,carid=records[num].carid;
while(carid==records[num].carid && num<recordsnum)
{
istr+=records[num+].second-records[num].second;
records[num+].second=-,records[num].second=-;
num+=;
}
records[num-].second=istr;
if(istr>flagout) flagout=istr;
} for(int i=;i<recordsnum;i++)
if(records[i].second==flagout) outlook(records[i].carid); //give the plate number of the car that has parked for the longest time period
printf("%02d:%02d:%02d",flagout/,(flagout/)%,flagout%); //and the corresponding time length
return ; }

PAT (Advanced Level) Practise - 1095. Cars on Campus (30)的更多相关文章

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

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

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

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

  3. PAT (Advanced Level) Practise - 1094. The Largest Generation (25)

    http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...

  4. PAT (Advanced Level) Practise 1004 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...

  5. PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)

    http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...

  6. PAT (Advanced Level) Practise - 1093. Count PAT's (25)

    http://www.patest.cn/contests/pat-a-practise/1093 The string APPAPT contains two PAT's as substrings ...

  7. 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...

  8. PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)

    http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...

  9. 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...

随机推荐

  1. Maven整合Eclipse进行Java项目开发

    一.Maven的配置 ①要求当前系统环境下配置了JAVA_HOME 在CMD命令行中输入:echo %JAVA_HOME% 如果能出来JDK的安装目录,说明我们配置了JAVA环境 ②将Maven的ZI ...

  2. [转]黑幕背后的__block修饰符

    http://www.cocoachina.com/ios/20150106/10850.html 我们知道在Block使用中,Block内部能够读取外部局部变量的值.但我们需要改变这个变量的值时,我 ...

  3. Unity---遇到的一些坑和解决方案

    目录 1.在UGUI中的物体顺时针旋转Z是负的.(和正常3D中是相反的) 2.MoveTowards()+Vector3.Distance()控制物体的移动 3.trtransform.SetPare ...

  4. react native项目在ios上运行测试,亲测

    参考文章:https://segmentfault.com/a/1190000014416132 说明:参考文章中有对AppDelegate.m文件的操作,我的RN版本是0.57.8未设置,也可成功运 ...

  5. 关于双端队列 deque 模板 && 滑动窗口 (自出)

    嗯... deque 即为双端队列,是c++语言中STL库中提供的一个东西,其功能比队列更强大,可以从队列的头与尾进行操作... 但是它的操作与队列十分相似,详见代码1: 1 #include < ...

  6. CentOS 安装最新的 RabbitMQ 3.7.8

    RabbitMQ依赖Erlang,Erlang又依赖很多包,安装非常麻烦,可以如下操作: RabbitMQ在github上有提供新的erlang包(https://github.com/rabbitm ...

  7. 【手撸一个ORM】第七步、SqlDataReader转实体

    说明 使用Expression(表达式目录树)转Entity的文章在园子里有很多,思路也大致也一样,我在前面有篇文章对解决思路有些说明,有兴趣的小伙伴可以看下 (传送门),刚接触表达式目录树时写的,不 ...

  8. 将本地代码添加到github

    首先在github上创建一个仓库. 第一步:建立本地仓库 git init 关联远程仓库 git remote add origin https://github.com/tshua/***.git ...

  9. 大数据“重磅炸弹”——实时计算框架 Flink

    Flink 学习 项目地址:https://github.com/zhisheng17/flink-learning/ 博客:http://www.54tianzhisheng.cn/tags/Fli ...

  10. 白话SpringCloud | 第二章:服务注册与发现(Eureka)-上

    前言 从本章节开始,正式进入SpringCloud的基础教程.从第一章<什么是SpringCloud>中我们可以知道,一个微服务框架覆盖的东西是很多的,而如何去管理这些服务或者说API接口 ...