PAT Advanced 1095 Cars on Campus: Sorting and De-duplication


PAT Advanced 1095 Cars on Campus: sorting and de-duplication, simulate to count current cars on campus, note a single car can come in and out for several times

PAT Advanced 1095 Cars on Campus

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

Sample Output:

JH007BD ZD00001 07:20:09

Solution: Sorting and De-duplication

There is not advanced algorithm involved in the problem, one can just simulate the process, checking each car to see whether it is IN or OUT, if it is IN, just add it to the total count of cars on campus for the query, if it is OUT, then just deduce one from the total count, the key information here is that the query is in ascending order. As a result, we do not have to repeat those that are already counted for the previous queries, so the time complexity for this part is acutally O(N) where N is the number of valid car records.

The second thing here is to filter out the valid records, the following are some key notes to keep in mind and do not fall into these traps:

  1. A single car could come in and out of the campus for more than one time, and the total parking time is the sum over all these parking periods.
  2. Do not forget to sort the car plate number in alphabetic order for those with the maximum parking time.

The following C++ code can pass the PAT OJ for this 1095 Cars on Campus problem:

#include <cstdio>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <utility>

using std::vector;
using std::make_pair;
using std::unordered_map;
using std::sort;
using std::string;
using std::pair;
using std::cin;

struct Record {
    string plateNumber;
    int time;
    bool in;
    Record(const string & n, int t, bool i) : plateNumber(n), time(t), in(i) {}
    Record() : plateNumber(""), time(-1), in(false) {}

bool comp(const Record &r1, const Record &r2) {
    return r1.time < r2.time;

bool comp2(const Record &r1, const Record &r2) {
    if (r1.plateNumber != r2.plateNumber)
        return r1.plateNumber < r2.plateNumber;
    return r1.time < r2.time;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    string plateNumber;
    char status[3];
    int h, m, s;
    int time;

    vector<Record> records;

    for (int i = 0; i < n; ++i) {
        cin >> records[i].plateNumber;
        scanf("%d:%d:%d %s", &h, &m, &s, status);

        records[i].time = h * 3600 + m * 60 + s;
        records[i].in = (status[0] == 'i' ? true : false);

    sort(records.begin(), records.end(), comp2);
    vector<Record> validRecords;
    unordered_map<string, int> validCarsParkingTime;
    int i1 = 0, i2 = 1;
    while (i2 < n) {
        if (records[i1].plateNumber == records[i2].plateNumber && records[i1].in && !records[i2].in) {

            const string & carNumber = records[i1].plateNumber;
            if (validCarsParkingTime.find(carNumber) == validCarsParkingTime.end() ) {
                validCarsParkingTime[carNumber] = (records[i2].time - records[i1].time);
            } else {
                validCarsParkingTime[carNumber] += (records[i2].time - records[i1].time);

            i1 += 2;
            i2 += 2;
        } else {
            ++i1, ++i2;

    sort(validRecords.begin(), validRecords.end(), comp);

    int j = 0;
    int len = validRecords.size();
    int total = 0;

    for (int i = 0; i < k; ++i) {
        scanf("%d:%d:%d", &h, &m, &s);

        time = h * 3600 + m * 60 + s;

        while (j < len && validRecords[j].time <= time) {
            if (validRecords[j].in) {
            } else {
        printf("%d\n", total);

    int maxParkingTime = -1;
    vector<string> maxParkingPlateNumberList;
    for (const auto&  kv: validCarsParkingTime ) {
        if (kv.second > maxParkingTime) {
            maxParkingTime = kv.second;
        } else if (kv.second == maxParkingTime) {


    sort(maxParkingPlateNumberList.begin(), maxParkingPlateNumberList.end());
    for (const auto & c : maxParkingPlateNumberList) {
        printf("%s ", c.c_str());
    if (maxParkingPlateNumberList.size() > 0) {
        printf("%.2d:%.2d:%.2d\n", maxParkingTime / 3600, maxParkingTime % 3600 / 60, maxParkingTime % 60);


PAT Advanced 1095 Cars on Campus: sorting and de-duplication, simulate to count current cars on campus, note a single car can come in and out for several times

Written on May 30, 2015