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:

  1. 16 7
  2. JH007BD 18:00:01 in
  3. ZD00001 11:30:08 out
  4. DB8888A 13:00:00 out
  5. ZA3Q625 23:59:50 out
  6. ZA133CH 10:23:00 in
  7. ZD00001 04:09:59 in
  8. JH007BD 05:09:59 in
  9. ZA3Q625 11:42:01 out
  10. JH007BD 05:10:33 in
  11. ZA3Q625 06:30:50 in
  12. JH007BD 12:23:42 out
  13. ZA3Q625 23:55:00 in
  14. JH007BD 12:24:23 out
  15. ZA133CH 17:11:22 out
  16. JH007BD 18:07:01 out
  17. DB8888A 06:30:50 in
  18. 05:10:00
  19. 06:30:50
  20. 11:00:00
  21. 12:23:42
  22. 14:00:00
  23. 18:00:00
  24. 23:59:00

Sample Output:

  1. 1
  2. 4
  3. 5
  4. 2
  5. 1
  6. 0
  7. 1
  8. 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级我才做三分之一不到 哈哈哈哈 说不定有更长的

  1. #include<cstdio>
  2. #include<cstring>
  3. struct carrecord
  4. {
  5. long long carid;
  6. int second;
  7. int status; // 1-in 0-out
  8. }records[];
  9. int recordsnum=,queriesnum=,time[][]={};
  10.  
  11. long long str2int(char *str)//一个大于 (26字母+10数字)的数值即可 用于保持比较时有效的字典序列
  12. {
  13. long long num=,istr=;
  14. while(str[istr]) num=num*+((''<=str[istr]&&str[istr]<='')?str[istr]-'':str[istr]-'A'+),istr++;
  15. return num;
  16. }
  17.  
  18. void outlook(long long num)//一条记录输出
  19. {
  20. char car[]="";
  21. int istr=,temp=;
  22. car[]='\0';
  23. while(istr>=)//int2str 恢复字符串名字
  24. {
  25. temp=num%;
  26. if(temp>=) car[istr]=temp+'A'-;
  27. else car[istr]=temp+'';
  28. num/=,istr--;
  29. }
  30. printf("%s ",car);
  31. }
  32.  
  33. //基于汽车牌号(以转为对应序列的数值)和进出时间的快排
  34. void QS(int low,int high,const int iqs) //1-carid 0-time
  35. {
  36. int l=low,h=high,second=records[l].second,status=records[l].status;
  37. long long carid=records[l].carid;
  38.  
  39. while(l<h)
  40. {
  41. if(iqs) while( l<h && (carid<records[h].carid ||(carid==records[h].carid && second<=records[h].second))) h--;
  42. else while( l<h && (second<records[h].second ||( carid<=records[h].carid && second==records[h].second ))) h--;
  43. if(l<h)
  44. {
  45. records[l].carid=records[h].carid;
  46. records[l].second=records[h].second;
  47. records[l].status=records[h].status;
  48.  
  49. records[h].carid=carid;
  50. records[h].second=second;
  51. records[h].status=status;
  52. }
  53.  
  54. if(iqs) while( l<h && (carid>records[l].carid||(carid==records[l].carid&& second>=records[l].second))) l++;
  55. else while( l<h && (second>records[l].second ||(carid>=records[l].carid&& second==records[l].second))) l++;
  56. if(l<h)
  57. {
  58. records[h].carid=records[l].carid;
  59. records[h].second=records[l].second;
  60. records[h].status=records[l].status;
  61.  
  62. records[l].carid=carid;
  63. records[l].second=second;
  64. records[l].status=status;
  65. }
  66. }
  67. if(low+<l) QS(low,l-,iqs);
  68. if(h+<high) QS(h+,high,iqs);
  69. }
  70.  
  71. int clean(int len,int in,int out)
  72. {
  73. int l=len,h=in;
  74. records[l].carid=records[h].carid;
  75. records[l].second=records[h].second;
  76. records[l].status=records[h].status;
  77.  
  78. l=l+,h=out;
  79. records[l].carid=records[h].carid;
  80. records[l].second=records[h].second;
  81. records[l].status=records[h].status;
  82.  
  83. return len+;
  84. }
  85.  
  86. int main()
  87. {
  88. scanf("%d%d",&recordsnum,&queriesnum);
  89.  
  90. char carname[],status[];
  91. int hh,mm,ss;
  92. for(int i=;i<=recordsnum;i++)//从1开始是为了方便下面记录配对时直接移动即可 而不需temp
  93. {
  94. scanf("%s %d:%d:%d %s",carname,&hh,&mm,&ss,status);
  95. records[i].carid=str2int(carname);// 比较、移动、复制等,操作简单,节省时间
  96. records[i].second=ss+*(mm+*hh);
  97. records[i].status=(==strcmp(status,"in")?:);
  98. }
  99. QS(,recordsnum,);// 基于汽车牌号(以转为对应序列的数值)的QS
  100.  
  101. int num=,len=,flagin=-,flagout=-;
  102. long long carid=;
  103. while(num<=recordsnum)// Each "in" record is paired with the chronologically next record for the same car provided it is an "out" record
  104. {
  105. flagin=-,flagout=-,carid=records[num].carid;
  106. 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.
  107. {
  108. if(==records[num].status) flagin=num,flagout=-;
  109. else if(-==flagin) flagin=-,flagout=-;
  110. else len=clean(len,flagin,num),flagin=-,flagout=-;
  111. num++;
  112. }
  113. }
  114. recordsnum=len;
  115. QS(,recordsnum-,);
  116.  
  117. num=;
  118. int istr=,carnum=,notfirst=;
  119. if(recordsnum) notfirst=;
  120. while(num<queriesnum) //For each query, output in a line the total number of cars parking on campus
  121. {
  122. scanf("%d:%d:%d",&hh,&mm,&ss);
  123. len=ss+*(mm+*hh);
  124. while(len>=records[istr].second && istr<recordsnum)
  125. {
  126. if(records[istr].status) carnum++;
  127. else carnum--;
  128. istr++;
  129. }
  130. if(num<queriesnum-notfirst) printf("%d\n",carnum);
  131. else printf("%d",carnum);
  132. num++;
  133. }
  134.  
  135. QS(,recordsnum-,);
  136. num=,istr=,flagout=;
  137. while(num<recordsnum) //the longest time period parked for
  138. {
  139. istr=,carid=records[num].carid;
  140. while(carid==records[num].carid && num<recordsnum)
  141. {
  142. istr+=records[num+].second-records[num].second;
  143. records[num+].second=-,records[num].second=-;
  144. num+=;
  145. }
  146. records[num-].second=istr;
  147. if(istr>flagout) flagout=istr;
  148. }
  149.  
  150. for(int i=;i<recordsnum;i++)
  151. if(records[i].second==flagout) outlook(records[i].carid); //give the plate number of the car that has parked for the longest time period
  152. printf("%02d:%02d:%02d",flagout/,(flagout/)%,flagout%); //and the corresponding time length
  153. return ;
  154.  
  155. }

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. 我的省选 Day -5

    Day -5 时间载着我们,一天又一天,呼啸而过. 已经记不清今天是Day 负几了,总之还有不到一个星期就要去参加选拔赛了. 写一下今晚做NOI2009的心路历程. T1题意有点绕,但很快看出是个二分 ...

  2. 树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

    3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ​ 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽 ...

  3. ch8 -- useLK

    useLK 光流法跟踪FAST角点 执行    ./useLK ../../data 运行程序. 光流法需要include<opencv2/video/tracking.hpp>,用到列表 ...

  4. emmet中的用法

    CSS Abbreviations Link VALUES LINK Emmet is about more than just HTML elements. You can inject value ...

  5. Javascript专题(一)严格模式

    严格模式是干什么的?它从哪里来? ECMAScript5中引入了严格模式.它为JS定义了一种不同的解析和执行模型. 摘自阮一峰博客:http://www.ruanyifeng.com/blog/201 ...

  6. 台州OJ 3709: Number Maze (数组越界不报RE,报WA坑爹)

    http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3709 You are playing on ...

  7. 存储过程 jdbc

    package com.itheima.procedure; import java.sql.CallableStatement; import java.sql.Connection; import ...

  8. BIO,NIO,AIO的理解

    BIO:同步阻塞式IO,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通过线程池机制改善. NIO: ...

  9. Oracle单列函数

    --字符函数--1.ASCII 返回与指定的字符对应的十进制数;select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space fr ...

  10. 面向对象设计与构造:JML规格单元作业总结

    面向对象设计与构造:JML规格单元作业总结 第一部分:JML语言理论基础 JML语言是什么:对Java程序进行规格化设计的一种表示语言 使用JML语言有什么好处: 用逻辑严格的规格取代自然语言,照顾马 ...