【PAT甲级 - C++题解】1095 Cars on Campus

本文针对PAT甲级考试中的一道经典题目——《校园内的汽车》进行了解析,介绍了如何通过数据结构和算法高效地统计特定时刻校园内的汽车数量,以及找出停留时间最长的汽车。

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1095 Cars on Campus (pintia.cn)
🔑中文翻译:校园内的汽车
📣专栏定位:为想考甲级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 (≤104), the number of records, and K (≤8×104) 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
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
题意

给定车辆进出校园的状态信息,格式如下:

plate_number hh:mm:ss status

第一部分 plate_number 表示车辆的编号,第二部分 hh:mm:ss 表示车辆进/出的时刻,第三部分 status 表示车辆是进来还是出去,0 表示进来,1 表示出去。

现在给定询问的时刻(按时间升序给出),要求我们计算该时刻及以前校园内车辆的数量并输出。还要计算这一天在校园内停留时间最长的车辆信息,这个答案可能不唯一,如果不唯一则按照编号字典序升序输出。

另外,需要注意的是题目给定的时刻一定都是同一天的,不存在跨天的情况。并且给定的信息中可能出现不合法的情况,同一辆车 in 需要和接下来出现的 out 相匹配,如果没有匹配的信息则被忽略,题目保证至少有一辆车的信息能够匹配上。

思路

具体思路如下:

  1. 用一个结构体来存储车辆的状态信息,并用一个哈希表来存储输入的车辆状态信息,每辆车可能会多次进出校园,所以哈希表的 valuevector 来存储。另外,需要注意的是我们这里存储的时间单位是秒,即要将给定的时间转换成秒后存入,方便后续的比较。
  2. 先将每个车辆中的信息按照时间升序排序,合法的信息筛选出来,删除那些不合法的信息,并用另外一个数组 events 来存储所有车辆的合法信息。
  3. events 中的信息按照时间升序排序,开始计算询问时刻及以前校园内停留的车辆数 sum 。我们这里采用动态的计算方法,从前往后遍历 events 数组,由于已经是按时间排好序,并且询问的时刻是按升序给定的。所以可以以询问的时刻为分界线,每次都只遍历到比当前询问时刻要小的那个信息,并且遍历过程中只要遍历的信息状态为 0 即有车辆进,则 sum++ ;反之,sum-- 。每次遍历完的 sum 都一定是当前询问时刻及以前学校停留的车辆数,直接输出即可。
  4. 计算当天车辆在学校停留的最长时间,由于答案可能不唯一,所以要先找出最长时间,再回过头找出与最长停留时间相等的车辆信息,并按车辆编号字典序升序输出。
代码
#include<bits/stdc++.h>
using namespace std;

//车辆进出状态结构体
struct Event
{
    int tm, s;   //分别表示时间以及状态

    //按时间升序排序
    bool operator<(const Event& e)const
    {
        return tm < e.tm;
    }
};

//计算该车辆在学校停留的总时间
int get(vector<Event> e)
{
    int res = 0;
    for (int i = 0; i < e.size(); i += 2)
        res += e[i + 1].tm - e[i].tm;
    return res;
}

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

    //输入每个车辆进出信息
    unordered_map<string, vector<Event>> ents;
    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;
        ents[id].push_back({ t,s });
    }

    //将合法的车辆信息筛选出来
    vector<Event> events;
    for (auto& item : ents)
    {
        auto& ets = item.second;
        sort(ets.begin(), ets.end());    //先按时间升序排个序

        //将合法车辆信息放到数组前面去
        int k = 0;
        for (int i = 0; i < ets.size(); i++)
            if (ets[i].s == 0)	//判断是否能够匹配
                if (i + 1 < ets.size() && ets[i + 1].s == 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].s == 0)  sum++;
            else sum--;
            k++;
        }

        printf("%d\n", sum);
    }

    //计算车辆在学校停留最长的时间
    int maxt = 0;
    for (auto& item : ents)
        maxt = max(maxt, get(item.second));

    //找出停留时间最长的车辆信息
    vector<string> ans;
    for (auto& item : ents)
        if (get(item.second) == maxt)
            ans.push_back(item.first);

    //按字典序升序排序
    sort(ans.begin(), ans.end());

    //输出结果
    for (int i = 0; i < ans.size(); i++)   printf("%s ", ans[i].c_str());
    printf("%02d:%02d:%02d\n", maxt / 3600, maxt % 3600 / 60, maxt % 60);

    return 0;
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值