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;
}
### 关于 PAT 甲级 1024 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 目,虽然具体目描述未直接给出,但从相似类型的目分析来看,这类目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问时,深度优先搜索(DFS)是一种常见的解策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值