PAT甲級 1095

題目 Cars on Campus

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.

解析

輸入的一組停車數據,從中獲取查詢時間內的停車數輛。

  1. 數據清理,連續進入的數據以最後一次進入爲準,連續離開的數據以第一次離開爲準

代碼

#include<bits/stdc++.h>

#define INF 1<<29

using namespace std;

struct node {
    string id;
    int time;
    bool flag;
};

int n, k;
vector<node> data;

bool cmp1(node a, node b) {
    if (a.id != b.id) {
        return a.id < b.id;
    } else {
        return a.time < b.time;
    }
}

bool cmp2(node a, node b) {

    return a.time < b.time;

}

void pat1095() {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        string id, flag;
        int hour, minute, second;
        cin >> id;
        scanf("%d:%d:%d", &hour, &minute, &second);
        cin >> flag;
        int time = hour * 3600 + minute * 60 + second;
        bool f = flag == "in";
        node n = {
                id, time, f
        };
        data.push_back(n);
    }

    sort(data.begin(), data.end(), cmp1);
    vector<node> result;
    vector<string> m;
    map<string, int> res_max;
    int max_time = -1;
    for (int i = 0; i < n - 1; ++i) {
        if (data[i].id == data[i + 1].id && data[i].flag == 1 && data[i + 1].flag == 0) {
            result.push_back(data[i]);
            result.push_back(data[i + 1]);
            res_max[data[i].id] += data[i + 1].time - data[i].time;
            max_time = max(res_max[data[i].id], max_time);
        }
    }
    sort(result.begin(), result.end(), cmp2);
    for (int i = 0; i < k; ++i) {
        int hour, minute, second;
        scanf("%d:%d:%d", &hour, &minute, &second);
        int time = hour * 3600 + minute * 60 + second;
        int sum = 0;
        int j=0;
        while (j<result.size() && result[j].time<=time){
            result[j].flag == 1?sum++:sum--;
            j++;
        }
        printf("%d\n", sum);
    }
    for (auto &it : res_max) {
        if (it.second == max_time)
            printf("%s ", it.first.c_str());
    }
    printf("%02d:%02d:%02d\n", max_time / 3600, max_time % 3600 / 60, max_time % 60);
}

int main() {
    pat1095();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章