#1095. Cars on Campus【模拟 + 字符串处理 + 哈希】

原题链接

Problem Description:

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 N N ( ≤ 1 0 4 \leq 10^4 104), the number of records, and K K K ( ≤ 8 × 1 0 4 \leq 8\times 10^4 8×104) the number of queries. Then N N 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 K 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

Problem Analysis:

注意题目的两个要求:

  1. 对于一些查询,计算出查询时刻校园内的汽车数量。
  2. 在一天结束时,找到在校园内停放时间最长的汽车。

对于问题1,由于我们需要忽略一些非法记录,即没有配对成功的记录 inour,我们可以开个 unordered_map 来将每个不同的汽车的所有非法记录都过滤掉,然后汇总到一个总的容器中按照时间顺序进行排序,并同时开个计数器,每当有一辆汽车进入,计数器加1,否则减1,这样即可计算出每个时刻的校园内汽车总数。

对于问题2,我们同样是遍历每个汽车的所有记录,然后累加求最大,再次遍历总记录,将所有汽车中停留时间等于最大值的汽车放进一个容器中,由于要按字典序输出,将容器排序输出即可。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <vector>

using namespace std;

struct Event
{
    
    
    int tm, status;
    bool operator< (const Event &t) const
    {
    
    
        return tm < t.tm;
    }
};

inline int get(vector<Event> ets)
{
    
    
    int res = 0;
    for (int i = 0; i < ets.size(); i += 2)
        res += ets[i + 1].tm - ets[i].tm;
    return res;
}

int main()
{
    
    
    // 将所有车的合法记录都按照时间顺序放在一块
    int n, m;
    scanf("%d%d", &n, &m);
    
    unordered_map<string, vector<Event>> cars;
    
    char id[10], status[10];
    for (int i = 0; i < n; i ++ )  // 读入每个事件
    {
    
    
        int hh, mm, ss;
        scanf("%s %d:%d:%d %s", id, &hh, &mm, &ss, status);
        int t = hh * 3600 + mm * 60 + ss;
        int s = 0;
        if (status[0] == 'o') s = 1;
        cars[id].push_back({
    
    t, s});  // 将所有车的信息归到一块
    }
    
    vector<Event> events;  // 所有合法记录混到一块的容器
    for (auto& item : cars)
    {
    
    
        auto& ets = item.second;
        sort(ets.begin(), ets.end());
        int k = 0;
        for (int i = 0; i < ets.size(); i ++ )
            if (ets[i].status == 0)
            {
    
    
                if (i + 1 < ets.size() && ets[i + 1].status == 1)
                {
    
    
                    ets[k ++ ] = ets[i];
                    ets[k ++ ] = ets[i + 1];
                    i ++ ;
                }
            }
        
        ets.erase(ets.begin() + k, ets.end());
        for (int i = 0; i < k; i ++ ) events.push_back(ets[i]);
    }
    sort(events.begin(), events.end());
    
    int k = 0, sum = 0;
    while (m -- )
    {
    
    
        int hh, mm, ss;
        scanf("%d:%d:%d", &hh, &mm, &ss);
        int t = hh * 3600 + mm * 60 + ss; 
        
        while (k < events.size() && events[k].tm <= t)
        {
    
    
            if (events[k].status == 0) sum ++ ;
            else sum -- ;
            k ++ ;
        }
        printf("%d\n", sum);
    }
    int maxt = 0;
    for (auto& item : cars) maxt = max(maxt, get(item.second));
    
    vector<string> res;
    for (auto& item : cars)
        if (get(item.second) == maxt)
            res.push_back(item.first);
    
    sort(res.begin(), res.end());
    
    for (int i = 0; i < res.size(); i ++ ) printf("%s ", res[i].c_str());
    
    printf("%02d:%02d:%02d\n", maxt / 3600, maxt % 3600 / 60, maxt % 60);
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/geraltofrivia123/article/details/121300651