码迷,mamicode.com
首页 > 其他好文 > 详细

1095. Cars on Campus (30)

时间:2015-12-06 13:07:08      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

(模拟题)

时间限制
220 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

 
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<map>
  6. #include<string>
  7. #include<string.h>
  8. #pragma warning(disable:4996)
  9. using namespace std;
  10. struct Recode {
  11. char card[8];
  12. char time[9];
  13. char statue[4];
  14. int timeTrans;
  15. int stat;
  16. };
  17. struct Car {
  18. int intime = -1;
  19. int outtime = -1;
  20. int staytime = 0;
  21. };
  22. int totalStatue[86401] = { 0 };
  23. map<string, Car> mp;
  24. vector<Recode> r;
  25. vector<string> longStay;
  26. int longStayTime = 0;
  27. bool cmp(Recode a,Recode b) {
  28. return a.timeTrans < b.timeTrans;
  29. }
  30. bool cmpCard(string a,string b) {
  31. return a < b;
  32. }
  33. int main(void) {
  34. freopen("Text.txt", "r", stdin);
  35. int n, m;
  36. cin >> n >> m;
  37. for (int i = 0; i < n; i++) {
  38. Recode temp;
  39. scanf("%s %s %s", temp.card, temp.time, temp.statue);
  40. if (temp.statue[0] == ‘i‘)
  41. temp.stat = 0;//in
  42. else
  43. temp.stat = 1;//out
  44. int ah, am, as;
  45. sscanf(temp.time, "%d:%d:%d", &ah, &am, &as);
  46. temp.timeTrans = ah * 3600 + am * 60 + as;
  47. r.push_back(temp);
  48. }
  49. sort(r.begin(), r.end(), cmp);
  50. for (int i = 0; i < n; i++) {
  51. string cardStr;
  52. int q = 0;
  53. while (r[i].card[q] != 0) {
  54. cardStr += r[i].card[q];
  55. q++;
  56. }
  57. if (r[i].stat == 0) {
  58. mp[cardStr].intime = r[i].timeTrans;
  59. }
  60. if (r[i].stat == 1) {
  61. if (mp[cardStr].intime == -1) {
  62. continue;
  63. }
  64. else {
  65. mp[cardStr].outtime = r[i].timeTrans;
  66. totalStatue[mp[cardStr].intime]++;
  67. totalStatue[mp[cardStr].outtime]--;
  68. mp[cardStr].staytime += (mp[cardStr].outtime - mp[cardStr].intime);
  69. mp[cardStr].outtime = -1;
  70. mp[cardStr].intime = -1;
  71. if (mp[cardStr].staytime > longStayTime) {
  72. longStay.clear();
  73. longStay.push_back(cardStr);
  74. longStayTime = mp[cardStr].staytime;
  75. }
  76. else if(mp[cardStr].staytime == longStayTime) {
  77. longStay.push_back(cardStr);
  78. }
  79. }
  80. }
  81. }
  82. int lastCheckTime = -1, hasCar = 0;
  83. for (int i = 0; i < m; i++) {
  84. char checkTime[9];
  85. scanf("%s", checkTime);
  86. int ch, cm, cs;
  87. sscanf(checkTime, "%d:%d:%d", &ch, &cm, &cs);
  88. int ctime = ch * 3600 + cm * 60 + cs;
  89. //cout << totalStatue[ctime] << endl;
  90. for (int j = lastCheckTime+1; j <= ctime; j++) {
  91. if (totalStatue[j] != 0) {
  92. hasCar += totalStatue[j];
  93. }
  94. }
  95. lastCheckTime = ctime;
  96. printf("%d\n", hasCar);
  97. }
  98. sort(longStay.begin(), longStay.end(), cmpCard);
  99. for (int i = 0; i < longStay.size(); i++) {
  100. cout << longStay[i] << " ";
  101. }
  102. int lh, lm, ls;
  103. lh = longStayTime / 3600;
  104. lm = longStayTime / 60 % 60;
  105. ls = longStayTime % 60;
  106. printf("%02d:%02d:%02d", lh, lm, ls);
  107. return 0;
  108. }





1095. Cars on Campus (30)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!