Cars On Campus

原题

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 ( N ≤ 10^​4 ), the number of records, and K ( K ≤ 8×10^4​​ ) the number of queries. Then N lines follow, each gives a record in the format: plate_number hh:mm:ss statuswhere 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:

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 accending 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

要求

实现:监测一个停车场里车辆的数量、停车时间,给出特定时间点场内车辆数,并在输入结束后给出停车时间最长的车的牌号(如有多辆,按照车牌号排序)以及停车时间。
输入限制:同一辆车不能在同一时间进出;同一辆车可以多次进出。


程序部分

定义与库

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 10000

typedef struct record* Car;
struct record{
    char plate_num[8];
    int total;
    int status;
};

typedef struct TM TIME;
struct TM
{
    int hour;
    int minute;
    int second;
};

sort部分

int comp(const void *a, const void *b)
{  
    if( strcmp( ((Car)a)->plate_num, ((Car)b)->plate_num ) == 0)
        return ((Car)a)->total - ((Car)b)->total;
    else 
        return strcmp( ((Car)a)->plate_num, ((Car)b)->plate_num );
}    

//the function for qsort to sort cars in accending order base on platenumber and total time

int comp2(const void *a, const void *b)
{  
    return ((Car)a)->total - ((Car)b)->total;
}

//the function for qsort to sort cars in accending order base on time

main函数

int main()
{
    int N, K, i, count = 0;
    int total, maxtime = 0, cartime;
    int num = 0, cur = 0;
    char precar[8];
    char status[4];
    char longest[MAXSIZE][8];
    Car car;
    TIME tm_query,tm_car,tm; 

    //input
    scanf("%d%d", &N, &K);
    car = (Car) malloc(sizeof(struct record) * N);

    for(i = 0; i < N; i++)
    {
        scanf("%s %d:%d:%d %s", car[i].plate_num, &tm_car.hour, &tm_car.minute, &tm_car.second, status);
        car[i].total = tm_car.hour*3600 + tm_car.minute*60 + tm_car.second;   
        if(status[0] == 'i')     
            car[i].status = 1;
        else
            car[i].status = -1;  
        //status = 1 represent in
        //status = -1 represent out
    }

特定时间点计数


    /*this part is to tell the number of cars parking on campus at any specific time point*/

    qsort(car, N, sizeof(car[0]), comp);

    //sort records in alphabetical order base on plate number at first
    //if the plate numbers are equal the base on total times, which means the record with early time is displayed in front

    strcpy(precar, car[0].plate_num);    

    //precar to store the platenumber of the car we are dealing with

    for(i = 0; i < N; )
    {
        if(car[i].status == 1 && i+1 != N && strcmp(car[i].plate_num, car[i+1].plate_num) == 0 && car[i+1].status == -1)
        {   
            //to make sure in and out are well-repaired

            if(strcmp(precar, car[i].plate_num) == 0)
            {
                 //if the previous car are the same with this one
                cartime += car[i+1].total - car[i].total;     
                //calculate the time this car have stayed
            }
            else
            {
                strcpy(precar, car[i].plate_num);    
                cartime = car[i+1].total - car[i].total;      
                //set a new time this car have stayed
            }
            if(maxtime < cartime)
            {      
                count = 0;    
                //clear the array "longest"
                strcpy(longest[count++], car[i].plate_num);   
                //store the platenumber of the longest car
                maxtime = cartime;    
                //record the maxtime 
            }
            else if(maxtime == cartime)
            {    
                // cars with the same stay period
                strcpy(longest[count++], car[i].plate_num);   
                //add it into the array
            }
            i += 2;     
            //pass this car
        }
        else
        {
            car[i].status = 0;   
            //set the status = 0 to represent this record is invalid
            i++;    
        }
    }

最长停车时间计算

/*THis part is to find the cars that have parked for the longest time period at the end of the day*/

qsort(car, N, sizeof(car[0]), comp2);

//set the array store query times in accending order

for(i = 0; i < K; i++)
    {
        scanf("%d:%d:%d", &tm_query.hour, &tm_query.minute, &tm_query.second);
        total = tm_query.hour*3600 + tm_query.minute*60 + tm_query.second;
        while(cur < N)
        {     
            //since the queries are given in accending order of times
            if(car[cur].total <= total)
            {    
                //just calculate the records before the time of each query
                num += car[cur].status;    
                //status of in record and out is 1 and -1,status of invalid record is 0
                cur++;                 
            }
            else 
                break;
        }
        printf("%d\n", num);
    }   
    // get the number of cars for all query times


   for(i = 0; i < count; i++)
    {
        printf("%s ", longest[i]);    
        //get the cars which have the longest stay time
    }                                   
    tm.hour = maxtime/3600;
    tm.minute = maxtime%3600/60;
    tm.second = maxtime%60;
    printf("%02d:%02d:%02d", tm.hour, tm.minute, tm.second);    
    //print the longest time

    return 0;
}

Test Case

1. Just one paired record

10 4
JH007BD 18:00:01 in
JH007BD 04:09:59 in
JH007BD 05:09:59 in
JH007BD 11:42:01 in
JH007BD 05:10:33 in
JH007BD 06:30:50 in
JH007BD 12:23:42 in
JH007BD 23:55:00 in
JH007BD 18:07:01 in
JH007BD 23:59:50 out
05:10:00
12:23:42
18:00:00
23:59:00
  1. Find the correct car which stays longest.
  2. The car number in the last test time is incorrect.

2. Cars in or out at the test time

9 5
JH007BD 05:00:01 in
DB8888A 13:00:00 out
ZA3Q625 23:59:50 out
ZD00001 10:23:00 out
ZD00001 01:09:59 in
ZA3Q625 11:42:01 out
ZA3Q625 06:30:50 in
JH007BD 12:23:42 out
DB8888A 06:30:50 in
05:00:01
06:30:50
13:00:00
12:23:42
23:59:00
  1. Find the correct car which stays longest.
  2. The car number in the last test time is correct.

3. More than one car stay longest

8 2
JH007BD 00:00:00 in
DB8888A 00:00:00 in
ZA3Q625 00:00:00 in
ZA133CH 00:00:00 in
JH007BD 00:01:00 out
DB8888A 00:01:00 out
ZA3Q625 00:01:00 out
ZA133CH 00:01:00 out
05:10:00
23:59:00

1. Find the correct car which stays longest.
2. The car number in the last test time is correct.


4. One car stays longest more than one time

8 2
JH007BD 00:00:00 in
JH007BD 00:02:00 in
JH007BD 00:04:00 in
JH007BD 00:08:00 in
JH007BD 00:01:00 out
JH007BD 00:03:00 out
JH007BD 00:07:00 out
JH007BD 00:011:00 out
05:10:00
23:59:00

1. Find the correct car which stays longest.
2. The car number in the last test time is correct.


5. Big Data

See more in attachment.

Actually, repeat the input simply.


Analysis

Space Complexity

The algorithm uses a structure which includes a dimensional array to store the informations for each records.

S(N)=Θ(N).(N is the number of records.)

Time Complexity

The time complexity of the qsort Algorithm is O(N*logN).

The worst case is that we always choose the bad pivot which will seperate the datas into two unbalanced parts. Therefore T(N)=O(N^2). (N is the number of records.)

Comment

There is a question that whether the time complexity of sort Algorithm is exactly O(N*logN).

Actually, the qsort Algorithm in C libraries isn’t Quick Sort simply. The algorithm in glibc uses a stack macro to avoid stack bursted during the recursion. What’s more, it will choose Insert Sort when the number of elements is less than 4. Also, it can find effective Pivot after optimization.

In a conclusion, this algorithm uses qsort to reduce running time.

To solve this problem,Bucket Sort may run well when cars move in nearly seperate times. And its time complecity can be reduced to O(N*(lonN-logM)). (M is the number of buckets.)

Attachment

Qsort in glibc

作者:丁冬
https://www.zhihu.com/question/39214230/answer/80244880
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值