PAT甲級 1095. Cars on Campus (30)

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; andstatus 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個車輛進出車庫的記錄信息,k個詢問,每個詢問輸出當前時間車庫中有多少輛車;

最後一行輸出在車庫停留時間最長的車(如果有多輛,按字典序排列),並輸出停留的時間。

題目解析

模擬題,具體思路代碼註釋中很清楚

題中給的便利條件

1、詢問按時間順序詢問

2、對於同一輛車不會在同一個時間點進入和“out”

坑點:

1、對於一輛車:如果該輛車按時間順序的記錄如下:in -> in -> in -> out -> out-> in -> in,那麼只有紅色字體的記錄是有效的

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;

const int MAXN = 1e4 + 10;
const int MAXK = 8e4 + 10;

struct NODE{
    string name;///string方便排序和用stl
    int time;///進入或出去時間
    bool status;///1:進入  0:出去
}node[MAXN];
int Cal_time(int x, int y, int z){
    return (x*3600+y*60+z);
}
bool cmp1(NODE a, NODE b){return a.time<b.time;}///時間排序
bool cmp2(NODE a, NODE b){
    if(a.name==b.name) return a.time<b.time;
    return a.name<b.name;
}///名字排序 時間小的在前面
bool cmp3(string a, string b){return a<b;}

int main(){
    int n, k;
    while(~scanf("%d%d", &n, &k)){
        int hh, mm, ss;
        for(int i=0; i<n; i++){
            char st[5];
            cin>>node[i].name;
            scanf("%d:%d:%d%s", &hh, &mm, &ss, st);
            node[i].time=Cal_time(hh, mm, ss);
            if(strcmp(st, "in")==0) node[i].status=true;///true爲進入
            else node[i].status=false;
        }

        sort(node, node+n, cmp2);///名字排序(時間小的在前面)
        int cnt=0;
        for(int i=0; i<n-1; i++){///排除不合法後的記錄數
            if(node[i].name==node[i+1].name && node[i].status && !node[i+1].status){
                node[cnt++]=node[i];
                node[cnt++]=node[i+1];
            }
        }

        sort(node, node+cnt, cmp1);///按時間排序
        int pos=0;///標記詢問過程中的斷點
        set<string>set_num;
        map<string, int>pre_time, cal_tim;
        while(k--){
            scanf("%d:%d:%d", &hh, &mm, &ss);
            int ti=Cal_time(hh, mm, ss);
            for(int i=pos; i<cnt; i++){
                string now=node[i].name;
                if(node[i].time>ti){pos=i;break;}///該條記錄的時間超過當前詢問的時間
                if(node[i].status){///進入
                    set_num.insert(now);
                    pre_time[now]=node[i].time;///記錄該車牌號進入的時間
                }else{
                    cal_tim[now]+=(node[i].time-pre_time[now]);
                    set_num.erase(now);
                }
                if(i==(cnt-1)) pos=cnt;
            }
            printf("%d\n", set_num.size());
        }

        for(int i=pos; i<cnt; i++){///詢問結束,但是車輛的進入信息還沒有計算完****(這點容易被忽略)
            string now=node[i].name;
            if(node[i].status) pre_time[now]=node[i].time;
            else cal_tim[now]+=(node[i].time-pre_time[now]);
        }
        int max_time=-1;
        vector<string>output;
        for(map<string, int>::iterator it=cal_tim.begin(); it!=cal_tim.end(); it++){///遍歷cal_tim求最大值
            string now=it->first;
            int now_ti=it->second;
            if(now_ti > max_time){
                output.clear();
                max_time=now_ti;
                output.push_back(now);
            }else if(now_ti == max_time) output.push_back(now);
        }

        sort(output.begin(), output.end(), cmp3);
        for(int i=0; i<output.size(); i++) cout<<output[i]<<' ';
        printf("%02d:%02d:%02d\n", max_time/3600, (max_time%3600)/60, max_time%60);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章