PAT 甲级 1095 Cars on Campus (30 分)

题目:
Zhejiang University has 8 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 (≤10
​4
​​ ), the number of records, and K (≤8×10
​4
​​ ) 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

题目大意:题意有点不清楚。。可能是我英语太渣。给出n个24小时内的停车信息,然后进行k个查询(查询时间递增,所以直接O(n)能过,降低了难度,本来想用二分来着)。对于每次查询,问你截止到目前时间点,校内停有多少辆车。注意:给的停车信息可能是有要忽略的,对于相同的车,最近的in和out匹配,多出来的in和out就要忽略。并且如果一辆车只有in或out也要忽略。

思路:由于in和out的顺序对结果也有影响,所以从一开始就要根据事件时间来进行排序。用若干个map来记录每辆车的状态,然后进行最近的in和out的匹配加入in数组和out数组,最后给两个数组排下序,用两个指针来求第一个大于该查询的时间点,两个指针相减就是答案,至于最后的输出,排一下序就行了。

AC Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
map<string,int> mp_in, mp_2, mp_ini;
vector<string> str,str1,str_s;
struct node
{
    string s,s1,s2;
} nd[10010];
bool cmp(node a,node b)
{
    return a.s1<b.s1;
}
int main ()
{
    int n,k,q,p;
    string s,s1,s2;
    scanf("%d%d",&n,&k);
    int maxx = 0;
    for(int i=0; i<n; i++)
        cin>>nd[i].s>>nd[i].s1>>nd[i].s2;

    sort(nd,nd+n,cmp);
    string ss = "";
    for(int i=0; i<n; i++)
    {
        ss = nd[i].s;
        if(nd[i].s2=="in")
        {
                mp_ini[ss] = i;
                mp_in[ss]=1;
        }
        else if(nd[i].s2=="out")
        {
                if(mp_in[ss]==1)  //in过
                {
                    str1.push_back(nd[i].s1);
                    str.push_back(nd[mp_ini[ss]].s1);
                    int sk = mp_ini[ss];
                    mp_in[ss]=0;
                    mp_2[ss] += ((((nd[i].s1[0]-'0')*10+(nd[i].s1[1]-'0'))*3600+((nd[i].s1[3]-'0')*10+
                                  (nd[i].s1[4]-'0'))*60+((nd[i].s1[6]-'0')*10+(nd[i].s1[7]-'0')))-
                                 (((nd[sk].s1[0]-'0')*10+(nd[sk].s1[1]-'0'))*3600+((nd[sk].s1[3]-'0')*10+
                                         (nd[sk].s1[4]-'0'))*60+((nd[sk].s1[6]-'0')*10+(nd[sk].s1[7]-'0'))));
                    if(maxx<mp_2[ss])
                        maxx = mp_2[ss];
                }
        }
    }
    int maxxx = maxx;
    int h,mins,si;
    h = maxxx/3600;
    maxxx = maxxx%3600;
    mins = maxxx/60;
    si = maxxx%60;
    sort(str.begin(),str.end());
    sort(str1.begin(),str1.end());
    string sq;
    q = 0,p = 0;
    while(k--)
    {
        cin>>sq;
        while(q<str.size()&&str[q]<=sq) q++;
        while(p<str1.size()&&str1[p]<=sq) p++;
        printf("%d\n",q - p);
    }
    map<string,int>::iterator it;
    for(it = mp_2.begin(); it!=mp_2.end(); it++)
    {
        if(it->second==maxx)
            str_s.push_back(it->first);
    }
    sort(str_s.begin(),str_s.end());
    for(int i = 0; i<str_s.size(); i++)
        cout<<str_s[i]<<" ";
    printf("%02d:%02d:%02d\n",h,mins,si);
    return 0;
}

总结:写完之后感觉还是挺简单的,但是理解题意和中间编码过程还是有磕碰,此题花了不少时间,多总结,多熟悉。

发布了22 篇原创文章 · 获赞 4 · 访问量 541

猜你喜欢

转载自blog.csdn.net/yp0413170331/article/details/100682176