【PAT 1006Cars on Campus (30)】

来源:互联网 发布:数据漏斗 excel 编辑:程序博客网 时间:2024/06/03 22:54

题目描述
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.

输入描述:
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.

输出描述:
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.

输入例子:
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

输出例子:
1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09

题意 : 给出一些车的 n 次进出记录,和 k 次查询,每次查询输出该时间点的车站内的停车个数,以及一天内停车的最长的时间,并字典序输出这些车

思路: 首先筛选出合法的记录,存在一些不合法的记录,有同一辆车多次连续的 in 时,取最后一次 in 的时间,多次连续 out 时取第一次的 out 时间,并同时统计出合法的停车时间,遍历一遍找出最长的时间,因为查询的时候时间点是递增的,把合法的in out 记录放在要一起 sort, 对于每次查询只需要遍历上一次的时间到这次的时间之间的进出车辆,然后加上之前的车辆

AC代码:

#include<cstdio>#include<map>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int MAX = 1e5 + 10;typedef long long LL;struct node{ char s[10],c[3]; int t;}st[MAX];struct nod{ int str,en;} no[MAX];struct noe{ int t,o; } nn[MAX];bool cmp(node i,node j){ return i.t < j.t; }bool cnp(string i,string j){ return i < j; }bool ccp(noe i,noe j) { return i.t < j.t; }map<string,int> m,mm;string sc[MAX];int main(){    int n,k,a,b,z;    while(~scanf("%d %d",&n,&k)){        int nl = 0;        m.clear(),mm.clear();        for(int i = 1; i <= n; i++){            scanf("%s%d:%d:%d%s",st[i].s,&a,&b,&z,st[i].c);            st[i].t = a * 60 * 60 + b * 60 + z;        }        sort(st + 1,st + 1 + n,cmp);        for(int i = 1; i <= n; i++){            if(st[i].c[0] == 'i') m[st[i].s] = st[i].t;            else if(m[st[i].s]){                    nod o;                    o.str = m[st[i].s],o.en = st[i].t;                    no[++nl] = o,mm[st[i].s] += st[i].t - m[st[i].s],m[st[i].s] = 0;            }        }        int ans = 0,p = 0;        for(int i = 1; i <= n; i++) ans = max(ans,mm[st[i].s]);        for(int i = 1; i <= n; i++) if(mm[st[i].s] == ans) sc[++p] = st[i].s,mm[st[i].s] = 0;        sort(sc + 1,sc + 1 + p,cnp);        int ll = 0;        for(int i = 1; i <= nl; i++){            noe o;            o.t = no[i].str,o.o = 1;            nn[++ll] = o;            o.t = no[i].en,o.o = 0;            nn[++ll] = o;        }        sort(nn + 1,nn + 1 + ll,ccp);        int u = 1,cut = 0;        while(k--){            scanf("%d:%d:%d",&a,&b,&z);            int tt = a * 60 * 60 + b * 60 + z;            while(nn[u].t <= tt && u <= ll){                if(nn[u].o) cut++;                else cut--;                u++;            }            printf("%d\n",cut);        }        for(int i = 1; i <= p; i++) cout << sc[i] << " ";        z = ans % 60,b = ans / 60 % 60,a = ans / (60 * 60);        printf("%02d:%02d:%02d\n",a,b,z);        }    return 0;}
原创粉丝点击