pat甲级-1095-Cars on Campus (30)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/richenyunqi/article/details/79313594

欢迎访问我的pat甲级题解目录哦https://blog.csdn.net/richenyunqi/article/details/79958195

1095. Cars on Campus (30)

时间限制
220 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Zhejiang University has 6 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 (<= 10000), the number of records, and K (<= 80000) 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

算法设计:

首先定义一个类Record存储车名、时间、停入停车场还是开出停车场的信息。然后在读取数据的时候我使用了unordered<string,vector<Record>>allCar来存储车名和其对应的记录,然后遍历allCar中每一个车名,对其对应的记录按时间排序,找出匹配的合法的停入开出记录,并存储在vector<Record>validCar中作为合法记录,在遍历过程中可顺便找出停入停车场时间最长的车和对应的停放时间。最后遍历validCar找出相应时间点停车场停放的车辆数,注意由于输入的查询时间是按时间排序的,为降低时间复杂度,每次查询可以从上次查询到的最后一个validCar的索引位置开始查询,保证查询时的时间复杂度为O(n),而不用每次都从头查起。

注意点:

  1. 查询时,如果查询的时间点恰好有车辆停入开出,也要被统计在内,例如样例中查询06:30:50停车场中的车辆数时,在06:30:50时间点恰好ZA3Q625车停入停车场,那么这辆车也要被统计在06:30:50停车场中的车辆中。
  2. 计算每辆车在停车场停放时间时,要统计总的停放时间,例如JH007BD在05:10:33到12:23:42停放:07:13:09,在18:00:01到18:07:01停放了00:07:00,故其在停车场停放时间为07:20:09
  3. 输出时时分秒要保证有两位数字,不够在高位补0
  4. 停放时间最长的车辆名要按字典序排序

c++代码:

#include<bits/stdc++.h>
using namespace std;
struct Record{
    char plateNum[8];//车名
    int time=0;//时间
    bool in=false;//停入还是开出
    Record(char*s,int t,bool i):time(t),in(i){//构造函数
        strcpy(plateNum,s);
    }
};
bool cmp(const Record&r1,const Record&r2){//比较函数
    return r1.time<r2.time;
}
int main(){
    int N,K;
    scanf("%d%d",&N,&K);
    unordered_map<string,vector<Record>>allCar;//存储车名与其对应的所有记录信息
    int h,m,s;
    while(N--){//读取数据
        char in[4],plate[8];
        scanf("\n%s %d:%d:%d %s",plate,&h,&m,&s,in);
        allCar[string(plate)].push_back(Record(plate,h*3600+m*60+s,strcmp(in,"in")==0));
    }
    vector<Record>validCar;//存储合法的记录信息
    set<string>maxPlate;//存储停放时间最长的车名,set可直接按字典序排序
    int maxTime=0;//存储停放的最长时间
    for(auto i=allCar.begin();i!=allCar.end();++i){//遍历allCar
        sort((i->second).begin(),(i->second).end(),cmp);//排序
        int t=0;//记录当前遍历到的车的停放时间
        for(int j=0;j<(i->second).size()-1;++j)//遍历记录信息
            if((i->second)[j].in&&!(i->second)[j+1].in){//如果当前记录与下一记录是匹配的
                validCar.push_back((i->second)[j]);//加入合法记录
                validCar.push_back((i->second)[j+1]);//加入合法记录
                t+=(i->second)[j+1].time-(i->second)[j].time;//累加停放时间
            }
        if(t>maxTime){//比较当前车辆停放时间与停放的最长时间,更新相关信息
            maxPlate.clear();
            maxPlate.insert(i->first);
            maxTime=t;
        }else if(t==maxTime)
            maxPlate.insert(i->first);
    }
    sort(validCar.begin(),validCar.end(),cmp);//对合法记录排序
    int parkCar=0,index=0;//当前停车场内的车辆数,索引位置
    while(K--){
        scanf("%d:%d:%d",&h,&m,&s);
        int t=(h*60+m)*60+s;//查询时间
        for(;index<validCar.size()&&validCar[index].time<=t;++index)//遍历合法记录
            parkCar+=validCar[index].in?1:-1;//统计停车场内车辆数
        printf("%d\n",parkCar);//输出
    }
    for(auto i=maxPlate.cbegin();i!=maxPlate.cend();++i)
        cout<<*i<<" ";
    printf("%02d:%02d:%02d",maxTime/3600,maxTime%3600/60,maxTime%60);
    return 0;
}

阅读更多

扫码向博主提问

日沉云起

用最短的代码,写最快的算法
  • 擅长领域:
  • 算法
去开通我的Chat快问
换一批

没有更多推荐了,返回首页