1095. Cars on Campus (30)——PAT (Advanced Level) Practise

Posted 闲云阁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1095. Cars on Campus (30)——PAT (Advanced Level) Practise相关的知识,希望对你有一定的参考价值。

题目信息

1095. Cars on Campus (30)

时间限制220 ms
内存限制65536 kB
代码长度限制16000 B
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

解题思路

时间处理

AC代码

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define MAX 10005
const int MAXRANGE = 24*60*60;
string decToTime(int time)
{
    char str[20];
    sprintf(str,"%02d:%02d:%02d",time/(60*60),time/60%60,time%60);
    return string(str);
}
int timeToDec(int h,int m,int s)
{
    return (h * 60 + m) * 60 + s;
}
class Car
{
public:
    string ID;
    int time;
    bool out;
public:
    bool isMatch(const Car &c)
    {
        return this->ID == c.ID&& this->time < c.time && this->out == false && c.out == true;
    }
    bool operator <(const Car &c) const
    {
        if (this->ID != c.ID)
        {
            return this->ID < c.ID;
        }
        else if (this->time != c.time)
        {
            return this->time < c.time;
        }
        else
        {
            return !this->out;
        }
    }
};
Car cars[MAX];
int sumCnt[MAXRANGE];
int main()
{
    int N, K;
    cin >> N >> K;
    for (int i = 0; i < N; i++)
    {
        int h, m, s;
        cin >> cars[i].ID;
        scanf("%d:%d:%d",&h,&m,&s);
        cars[i].time = timeToDec(h,m,s);
        string status;
        cin >> status;
        if (status == "out")
        {
            cars[i].out = true;
        }
        else
        {
            cars[i].out = false;
        }
    }
    sort(cars,cars+N);

    map<string, int> parkTime;
    int maxParkTime = -1;
    for (int i = 0; i < N-1; i++)
    {
        if (cars[i].isMatch(cars[i+1]))
        {
            parkTime[cars[i].ID] += cars[i + 1].time - cars[i].time;
            if (maxParkTime < parkTime[cars[i].ID])
            {
                maxParkTime = parkTime[cars[i].ID];
            }
            sumCnt[cars[i].time]++;
            sumCnt[cars[i+1].time]--;
        }
    }
    for (int i = 1; i < MAXRANGE; i++)
    {
        sumCnt[i] += sumCnt[i-1];
    }
    for (int i = 0; i < K; i++)
    {
        int h, m, s;
        scanf("%d:%d:%d",&h,&m,&s);
        printf("%d\n",sumCnt[timeToDec(h,m,s)]);
    }
    map<string, int>::iterator it;
    for (it = parkTime.begin(); it != parkTime.end(); ++it)
    {
        if (it->second == maxParkTime)
        {
            cout << it->first << " ";
        }
    }
    cout << decToTime(maxParkTime)<< endl;
    return 0;
}

以上是关于1095. Cars on Campus (30)——PAT (Advanced Level) Practise的主要内容,如果未能解决你的问题,请参考以下文章

A1095 Cars on Campus (30)(30 分)

1095 Cars on Campus (30分)(排序)

1095 Cars on Campus(30 分

1095 Cars on Campus (30 分)难 / 模拟 未完成

1095. Cars on Campus (30)——PAT (Advanced Level) Practise

1095. Cars on Campus (30)——PAT (Advanced Level) Practise