PAT A1095 Cars on Campus (30分) (模拟问题)

PAT甲级:A1095 Cars on Campus (30分)

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
  • 题意:浙江大学有8个校区和多个大门,每个门口都会记录车辆的进出时间,现在给定一天内车辆进出记录,要求根据指定时间输出当时的停车数量,最后输出停车时长最久的车辆。注:每辆车的进出记录都是匹配的,那些未匹配的记录将被舍弃。同时保证没有车辆在同一时刻进出。
  • 分析:因为存在无效的记录,因此先把有效记录筛选出来,同时记录停车时长最久的车牌号。之后将有效记录按照时间顺序排序。最后,查询某一时刻的停车数时,如果总是遍历一遍记录,那不用试,肯定会超时的,所以先遍历一遍有效记录,将每个时刻的车辆数都记录下来,最后使用二分查找查找。记录数量的方式:在结构体中用flag将进和出分别标记为1和-1,那么遍历记录时,因为已经按照时间增序,所有flag的累加和就是当前时间时间点的车辆数。

注意点:把输入的时间全转为秒,便于计算;最后输出的时间都是占两格的。二分查找的时候要如果查找成功,直接输出mid时刻的车辆数即可,如果查找失败,那应该输出小于所要查找时间的最大时刻记录下的数量,也就是right的当前位置。

最耗时的样例用了75ms,应该不算差了。

#include <bits/stdc++.h>
using namespace std;
struct node {
    string name;
    int time, flag;
};
bool cmp(node &a, node &b) {
    return a.name != b.name ? a.name < b.name : a.time < b.time;
}
bool cmpByTime(node &a, node &b) { return a.time < b.time; }
int main() {
    int n, k, hh, mm, ss, maxP = -1, tempCnt = 0;
    string status;
    scanf("%d%d", &n, &k);
    vector<node> record(n), validRecord;
    for (int i = 0; i < n; i++) {
        cin >> record[i].name;
        scanf("%d:%d:%d", &hh, &mm, &ss);
        cin >> status;
        record[i].time = hh * 3600 + mm * 60 + ss;
        record[i].flag = status == "in" ? 1 : -1;
    }
    sort(record.begin(), record.end(), cmp);
    vector<string> ans;
    unordered_map<string, int> mp;
    for (int i = 0; i < n - 1; i++) {
        if (record[i].name == record[i + 1].name &&
            record[i].flag == 1 && record[i + 1].flag == -1) {
            validRecord.push_back(record[i]);
            validRecord.push_back(record[i + 1]);
            mp[record[i].name] += record[i + 1].time - record[i].time;
            int ptime = mp[record[i].name];
            if (ptime > maxP) {
                maxP = ptime;
                ans.clear();
                ans.push_back(record[i].name);
            } else if (ptime == maxP) {
                ans.push_back(record[i].name);
            }
        }
    }
    sort(validRecord.begin(), validRecord.end(), cmpByTime);
    unordered_map<int, int> cnt;
    for (int i = 0; i < validRecord.size(); i++) {
        tempCnt += validRecord[i].flag;
        cnt[validRecord[i].time] = tempCnt;
    }
    while (k--) {
        scanf("%d:%d:%d", &hh, &mm, &ss);
        int time = hh * 3600 + mm * 60 + ss;
        int left = 0, right = validRecord.size() - 1, mid, flag = 0;
        while (left <= right) {
            mid = (left + right) / 2;
            if (time < validRecord[mid].time) right = mid - 1;
            else if (time > validRecord[mid].time) left = mid + 1;
            else {
                flag = 1;
                printf("%d\n", cnt[validRecord[mid].time]);
                break;
            }
        }
        if (!flag) printf("%d\n", cnt[validRecord[right].time]);
    }
    sort(ans.begin(), ans.end());
    for (auto it : ans) printf("%s ", it.c_str());
    printf("%02d:%02d:%02d", maxP / 3600, (maxP % 3600) / 60, maxP % 60);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值