PAT Advanced Level 1095
Posted absolutelyperfect
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT Advanced Level 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
超时,好辛酸, 改了又改还是超时,果然三十分不是白得的,今天一定要搞定。
先粘超时代码:
/********************** author: yomi date: 18.5.6 ps:很悲伤的故事 我一直纳闷为什么JH007BD能输出?原来算的是总时间呀 JH007BD 7:13:9 JH007BD 0:7:0 **********************/ #include <iostream> #include<cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; struct car { char no[10]; int h; int m; int s; char q[10]; //bool flag; }c[10010], real[10010]; bool cmp(car a, car b) { if(strcmp(a.no, b.no)!=0) return strcmp(a.no, b.no)<0; if(a.h != b.h) return a.h < b.h; else if(a.m != b.m) return a.m < b.m; else if(a.s!=b.s) return a.s<b.s; } bool cmp1(car a, car b) { if(a.h != b.h) return a.h < b.h; else if(a.m != b.m) return a.m < b.m; else if(a.s!=b.s) return a.s<b.s; } int main() { int n, t, lh=-1, lm=-1, ls=-1, longtim = 0; map<string, int>longtime; longtime.clear(); scanf("%d%d", &n, &t); for(int i=0; i<n; i++){ scanf("%s%d:%d:%d%s", c[i].no, &c[i].h, &c[i].m , &c[i].s, c[i].q); } sort(c, c+n, cmp); // for(int i=0; i<n; i++){ // cout << c[i].no << ‘ ‘<< c[i].h <<":"<< c[i].m << ‘:‘<< c[i].s<<‘ ‘ << c[i].q << endl; // } int j = 0; int cnt = 0; int cnt1 = 0; int temp = 0; while(j<n){ if(strcmp(c[j].q, "in") == 0) break; j++; } int cnt2 = 0; while(j<n){ if(strcmp(c[j].no, c[j-1].no) == 0 && strcmp(c[j].q, "out") == 0&&strcmp(c[j-1].q, "in") == 0){ // c[j-1].flag = true; // c[j].flag = true; real[cnt++] = c[j-1]; real[cnt++] = c[j]; int h1,h2, m1, m2, s1, s2; h1 = real[cnt-2].h;h2=real[cnt-1].h; m1 = real[cnt-2].m;m2=real[cnt-1].m; s1 = real[cnt-2].s;s2=real[cnt-1].s; int time = 0; while(h1<h2 || m1<m2 || s1<s2){ s1++; if(s1 == 60){ s1 = 0; m1++; } if(m1 == 60){ m1 = 0; h1++; } time++; } string s = real[cnt-1].no; longtime[s] += time; if(longtim<longtime[s]){ longtim = longtime[s]; } } j++; } // for(int i=0; i<n; i++){ // cout << c[i].no << ‘ ‘<< c[i].h <<":"<< c[i].m << ‘:‘<< c[i].s<<‘ ‘ << c[i].q << endl; // } // for(int i=0; i<cnt; i++){ // cout << real[i].no << ‘ ‘<< real[i].h <<":"<< real[i].m << ‘:‘<< real[i].s<<‘ ‘ << real[i].q << ‘ ‘ << endl; // } /* int cnt1 = 0; TLE int temp = 0; for(int i=0; i<n; i+=2){ //cout << real[i].no << ‘ ‘<< real[i].h <<":"<< real[i].m << ‘:‘<< real[i].s<<‘ ‘ << real[i].q << ‘ ‘ << endl; int h1,h2, m1, m2, s1, s2; h1 = real[i].h;h2=real[i+1].h; m1 = real[i].m;m2=real[i+1].m; s1 = real[i].s;s2=real[i+1].s; int time = 0; while(h1<h2 || m1<m2 || s1<s2){ s1++; if(s1 == 60){ s1 = 0; m1++; } if(m1 == 60){ m1 = 0; h1++; } time++; } if(i+2<n && strcmp(real[i].no, real[i+2].no) == 0){ temp +=time; continue; } else{ time += temp; temp = 0; } strcpy(longtime[cnt1].no, real[i].no); longtime[cnt1].s = time%60; longtime[cnt1].h = time/3600; longtime[cnt1].m = (time-longtime[cnt1].s-longtime[cnt1].h*3600)/60; if(longtime[cnt1].h > lh){ lh = longtime[cnt1].h; lm = longtime[cnt1].m; ls = longtime[cnt1].s; } else if(longtime[cnt1].h == lh&&longtime[cnt1].m > lm){ lh = longtime[cnt1].h; lm = longtime[cnt1].m; ls = longtime[cnt1].s; } else if(longtime[cnt1].h == lh&& longtime[cnt1].m == lm&&longtime[cnt1].s > ls){ lh = longtime[cnt1].h; lm = longtime[cnt1].m; ls = longtime[cnt1].s; } cnt1++; }*/ // for(int i=0; i<cnt1; i++){ // cout << longtime[i].no << ‘ ‘ << longtime[i].h << ":" << longtime[i].m << ":"<<longtime[i].s << endl; // } int th, tm, ts; sort(real, real+cnt, cmp1); // for(int i=0; i<cnt; i++){ // cout << real[i].no << ‘ ‘<< real[i].h <<":"<< real[i].m << ‘:‘<< real[i].s<<‘ ‘ << real[i].q << ‘ ‘ << endl; // } j = 0; int ans = 0; for(int i=0; i<t; i++){ scanf("%d:%d:%d", &th, &tm, &ts); // while(j<cnt){ if(real[j].h > th){ //j--; break; } else if(real[j].h == th&&real[j].m > tm){ //j--; break; } else if(real[j].h == th&& real[j].m == tm&&real[j].s > ts){ //j--; break; } if(strcmp(real[j].q, "in") == 0) ans++; else if(strcmp(real[j].q, "out") == 0) ans--; j++; } printf("%d\n", ans); } //cout << "1" << endl; TLE // for(map<string, int>::iterator iter=longtime.begin(); iter!=longtime.end(); ++iter){ // int time = iter->second; // //cout << iter->first << ‘ ‘ << iter->second << endl; // if(time > longtim){ // ls = time%60; // lh = time/3600; // lm = (time-ls-lh*3600)/60; // longtim = time; // // } // //printf("%d:%d:%d\n", lh, lm, ls); // } //cout << "2" << endl; for(map<string, int>::iterator iter=longtime.begin(); iter!=longtime.end(); ++iter){ int time = iter->second; if(time == longtim){ //printf("%s ", iter->first); //cout << iter->first << ‘ ‘; const char *p = iter->first.c_str(); printf("%s ", p); } } ls = longtim%60; lh = longtim/3600; lm = (longtim-ls-lh*3600)/60; printf("%02d:%02d:%02d", lh, lm, ls); return 0; } /** 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 **/
以上是关于PAT Advanced Level 1095的主要内容,如果未能解决你的问题,请参考以下文章
PAT (Advanced Level) 1095. Cars on Campus (30)
PAT (Advanced Level) 1075. PAT Judge (25)
PAT (Advanced Level) 1025 PAT Ranking