#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符串哈希滑动窗口是一种用于处理字符串的算法。它主要用于在给定的字符串中找到满足特定条件的子串。 在字符串哈希滑动窗口算法中,我们首先计算原始字符串哈希值。然后,我们使用一个滑动窗口来遍历字符串,每次滑动一个固定长度的窗口。我们可以通过比较每个窗口内的子串的哈希值来判断是否满足条件。 具体而言,我们可以使用BKDRHash等哈希函数来计算字符串哈希值。然后,我们枚举每个可能的起点,并使用滑动窗口来计算窗口内的子串的哈希值。通过比较窗口内的子串的哈希值,我们可以判断是否满足条件。 对于滑动窗口的移动,如果窗口内的子串满足条件,我们可以继续将窗口往右移动一个固定的长度。如果窗口内的子串不满足条件,我们将窗口的右边界移到最右端,并依次比较新窗口内的子串的哈希值。 综上所述,字符串哈希滑动窗口算法是通过计算字符串哈希值,并使用滑动窗口来遍历字符串,以找到满足特定条件的子串。这个算法可以高效地处理字符串,并且能够应用于各种字符串相关的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [String (字符串哈希+滑动窗口)](https://blog.csdn.net/weixin_43872264/article/details/107571742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【字符串hash+滑动窗口】String HDU - 4821](https://blog.csdn.net/qq_45599865/article/details/111143633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值