PAT-A1095-Cars on Campus

PAT 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.

输入描述

 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.

输出描述

 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.


示例:

输入

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

输出

1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09


解题思路

 1.定义车辆的结构数组,用于存储车辆的状态(in或者是out),还有车辆的车牌号(id),进入的时间time,这里注意需要将time单位全部转换为秒,首先就是将题目给的数据读入啦。
 2.数据读入后,需要将结构数组进行排序,按照车牌的id和时间进行排序,这样可以证相同的车辆数据是在一起的。
 3.由于题意需要对只进不出或者是,只出不进的车辆数据进行剔除,因为本题目没有给截止时间,进行剔除后,将有效的数组放入到Valid结构数组中,构成新的数据,并在剔除过程中将存储更新最长的时间,同时还需要使用map存储每个车牌的当前停留时间最大值。
 4.开始遍历查询,查询是有序的,可以利用这一点完成一个累加,不需要重新从头查询,对程序进行优化,不然数据量比较大的时候就容易超时,最后将结果进行输出就好。
 5.最后使用一个map的迭代器,匹配到最大的数值,就可以输出,程序到此结束。

参考代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

struct Cars {
    char id[20];
    int time;
    char status[5];
} All[10010], Valid[10010];

//这里进行一个巧妙地写法,全部转换为s位单位,会方便很多
int TimeToInt(int hh, int mm, int ss) {
    return hh * 3600 + mm * 60 + ss;
}
//对原始数据进行排序
bool cmp(struct Cars a, struct Cars b) {
    if (strcmp(a.id, b.id)) { return strcmp(a.id, b.id) < 0; }
    else { return a.time < b.time; }
}
//对需要处理后的数据按照时间进行排序,方便后续时间的递增
bool cmpByTime(struct Cars a, struct Cars b) {
    return a.time < b.time;
}
//使用map来存储对应车,停留的时间
map<string, int> parkTime;

int main() {
    int n, query;
    scanf("%d%d",&n,&query);
    int hh, mm, ss;
    for (int i = 0; i < n; ++i) {
        scanf("%s %d:%d:%d %s", All[i].id, &hh, &mm, &ss, All[i].status);
        All[i].time = TimeToInt(hh, mm, ss);
    }
    sort(All, All + n, cmp);
    int num = 0, maxTime = -1;
    for (int i = 0; i < n - 1; ++i) {
        if (!strcmp(All[i].id, All[i + 1].id) && !strcmp(All[i].status, "in") && !strcmp(All[i + 1].status, "out")) {//剔除无效的数据 加入新的数组中
            Valid[num++] = All[i];
            Valid[num++] = All[i + 1];
            int inTime = All[i + 1].time - All[i].time;
            if (parkTime.count(All[i].id) == 0) { parkTime[All[i].id] = 0; } //初始化map
            parkTime[All[i].id] += inTime; //车辆可能多次进出 时间需要累加
            maxTime = max(maxTime, parkTime[All[i].id]); //最大值更新
        }
    }
    sort(Valid, Valid + num, cmpByTime);
    int now = 0, carsnum = 0;
    for (int i = 0; i < query; ++i) { //开始查询服务
        scanf("%d:%d:%d", &hh, &mm, &ss);
        int Time = TimeToInt(hh, mm, ss);
        while (now < num && Valid[now].time <= Time) { //查询记录的量不能超过数据量的最大值 并且时间不能超过查询时间
            if (!strcmp(Valid[now].status, "in")) {
                carsnum++;
            } else {
                carsnum--;
            }
            ++now;
        }
        printf("%d\n", carsnum);
    }
    map<string, int>::iterator it; //使用迭代器遍历数据 将最大值对应的键的输出
    for (it = parkTime.begin(); it != parkTime.end(); ++it) {
        if (it->second == maxTime) {
            printf("%s ", it->first.c_str());
        }
    }
    printf("%02d:%02d:%02d\n", maxTime / 3600, maxTime % 3600 / 60, maxTime % 60);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值