PAT A级1095 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 (≤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.

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

题目大意:输入N个车辆信息,K个查询信息,要求给出k次查询时间停车场里的车辆数,最后一行给出停车时间最长的车牌号和停车时长,如果有多俩则同行输出。

解题思路和注意点

此题和 A1016 phoneBills 有异曲同工之处,首先先要对车辆的信息进行排序,按照id升序、时间升序来排序,然后将有效数据存入另一个数组方便处理,再在valid[]数组里配对,当连续遇到“in”和“out”时将停车时长记录下来,用于求取最大停车时长。
这一题对于时间的要求比较严格,建议用scanf和printf,我第一次写只对两个答案,三个超时和一个答案错误,在不断调试之后找到了我的原因。
先贴上代码:

#include<bits/stdc++.h>
using namespace std;

map<string,int> parkTime;
const int maxn=10010;
int num=0;

struct Car{
char id[8];
int time;//统一使用秒做单位
char state[4];
}all[maxn],valid[maxn];

int getTime(int hh,int mm,int ss){
return ss+mm*60+hh*3600;
}

bool cmpByIdAndState(Car a,Car b){
    if(strcmp(a.id,b.id))return strcmp(a.id,b.id)<0;
    else return a.time<b.time;
}

bool cmpByTime(Car a,Car b){
    return a.time<b.time;
};

int main()
{
    int n,k,hh,mm,ss;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%s %d:%d:%d %s",all[i].id,&hh,&mm,&ss,all[i].state);
        all[i].time=getTime(hh,mm,ss);
    }

    sort(all,all+n,cmpByIdAndState);
    
    int maxParkTime=-1;

    for(int i=0;i<n-1;i++){
        if(!strcmp(all[i].id,all[i+1].id)//同一车牌
           && !strcmp(all[i].state,"in")//第一个是in
           && !strcmp(all[i+1].state,"out")//第二个是out
           )
        {
            valid[num++]=all[i];
            valid[num++]=all[i+1];

            int tempParkTime;
            tempParkTime=all[i+1].time-all[i].time;//计算出停车时间
            if(!parkTime.count(all[i].id))parkTime[all[i].id]=0;
            parkTime[all[i].id]+=tempParkTime;
            maxParkTime=max(maxParkTime,parkTime[all[i].id]);
        }
    }

    sort(valid,valid+num,cmpByTime);

            int now=0,parkCount=0;
    for(int i=0;i<k;i++){
        scanf("%d:%d:%d",&hh,&mm,&ss);
        int time=getTime(hh,mm,ss);

        while(now<num && valid[now].time<=time){
            if(!strcmp(valid[now].state,"in"))parkCount++;
            else parkCount--;
            now++;
        }

        printf("%d\n",parkCount);

    }


    map<string,int>::iterator it;
    for(it = parkTime.begin(); it != parkTime.end(); it++)
        if(it->second==maxParkTime)printf("%s ",it->first.c_str());

    printf("%02d:%02d:%02d\n",maxParkTime/3600,maxParkTime%3600/60,maxParkTime%60);

    return 0;
}

跳过坑点思路:我第一次提交时定义变量语句如int hh,mm,ss;是写在for循环语句里的,这样就会使语句的执行次数增多,在我把所有的定义语句都拿出来之后,不存在超时的问题了,但还有一个答案错误。我检查之后发现我的比较函数是这么写的:

bool cmpByTime(Car a,Car b){
   if(a.time!=b.time) return a.time<b.time;
};

后来思考过后觉得应该这么写:

bool cmpByTime(Car a,Car b){
    return a.time<b.time;
};

然后代码就通过啦。

总结一下:定义语句应该养成好习惯定义在for循环外面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值