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 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)的更多相关文章
- PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)
题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...
- PAT (Advanced Level) 1095. Cars on Campus (30)
模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...
- 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 ...
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 一起来造一个RxJava,揭秘RxJava的实现原理
一类创业者基本都是做传统行业的,这类创业者非常大胆,也非常舍得投入.很多时候他们如果看到或者想到一个商机,就会投入成千上百万,先把产品做出来,然后再去想怎么开拓市场. 这类传统行业的老板,问我最多的问 ...
- 单机部署zookeeper、kafka
先安装jdk:mkdir /usr/javatar xf jdk-8u151-linux-x64.tar.gzmv jdk1.8.0_151/ /usr/java/jdk1.8 cat /etc/pr ...
- emmet中的用法
CSS Abbreviations Link VALUES LINK Emmet is about more than just HTML elements. You can inject value ...
- React方法论
按照目前学习进度不定更新 react渲染的效率,看起来是全体的渲染,其实react在虚拟dom上的处理简直完美.它会过滤掉那些原来就有的东西,不去全体地重复渲染一遍. 即将进入实战,React至今的个 ...
- 如何创建width与height比例固定的元素
面试题,刚在github上看到的,说说这里面的知识点吧~~ padding-bottom的值,其百分比是根据元素自身的width来算的. padding,在标准盒模型中,width+padding+b ...
- 【密码学】Https握手协议以及证书认证
1. 什么是https Https = http + 加密 + 认证 https是对http的安全强化,在http的基础上引入了加密和认证过程.通过加密和认证构建一条安全的传输通道.所以https可以 ...
- C#数据库(MSSQL)帮助类
/// <summary> /// 数据库帮助类 /// <author>Devin</author> /// </summary> public se ...
- java-类的定义和用法
1.类的定义 public class Human{ }//每个源文件必须也只能有一个public类 class boy{ }//可以定义多个class类 class girl{ } 上面的类定义好后 ...
- oracle报错:ORA-01658(转自52斋347)
在oracle里创建表,报出错:ORA-01658: 无法为表空间space中的段创建 INITIAL 区:或者: ORA-01658: unable to create INITIAL extent ...
- 私有npm下载资源
私有npm库下载资源需要用户名和密码,这个需要创建npm库的人提供. 使用方法: npm login --registry=仓库地址 Username: 用户名 Password: 密码 Email: ...