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 ...
随机推荐
- thinkphp5更新时验证数据
在编辑页面form表单中添加一个隐藏域:<input type="hidden" name="表中id字段名" value="get方式传过来的 ...
- springboot 简单自定义starter - dubbo
首先需要引入pom 这里使用nacos注册中心 所以引入了nacos-client 使用zookeeper注册中心的话需要引入其相应的client <dependency> <gro ...
- 什么是obj文件?
百度百科: 程序编译时生成的中间代码文件.目标文件,一般是程序编译后的二进制文件,再通过链接器(LINK.EXE)和资源文件链接就成可执行文件了.OBJ只给出了程序的相对地址,而可执行文件是绝对地址. ...
- Codeforces 1161B(判断旋转对称)
要点 外层暴力枚举转的"角度",会发现肯定是n的约数 对于m条线段想判定当前的"角度"是否ok,每个线段只要管它自己的下一个即可,不必画个圈遍历一遍 之后将本来 ...
- Polycarp's Pockets(思维)
Polycarp has nn coins, the value of the ii-th coin is aiai. Polycarp wants to distribute all the coi ...
- Unity 切换场景的时候让某个游戏对象不消失
DontDestroyOnLoad(要操作的GanmeObject); 放在Start方法里就行
- 牛客网Java刷题知识点之四种不同的方式创建线程
不多说,直接上干货! 有4种方式可以用来创建线程: 第一种:继承Thread类,重写run方法 第二种:实现Runnable接口,并实现该接口的run方法(一般我们在编程的时候推荐用这种) 第三种:实 ...
- Hadoop 3节点集群无法成功启动zookeeper
今天在集群上跑程序的时候遇到了zookeeper无法成功启动的问题,先分别启动了主节点和从节点的zookeeper进程,并且通过jps也看到zookeeper进程已经启动了,但通过指令查看进程状态的时 ...
- arch安装软件提示包损坏
错误:lib32-libjpeg6-turbo: signature from "Colin Keenan <colinnkeenan@gmail.com>" is u ...
- c# string.format 的简写 $
var name = "huchao"; var info = $"你是谁,我叫:{name}"; Console.Write(info); Console.R ...