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;
}
时间: 2024-10-05 04:19:21

1095. Cars on Campus (30)——PAT (Advanced Level) Practise的相关文章

1004. Counting Leaves (30)——PAT (Advanced Level) Practise

题目信息: 1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contai

1014. Waiting in Line (30)——PAT (Advanced Level) Practise

题目信息: 1014. Waiting in Line (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rule

PAT 1095. Cars on Campus (30)

1095. Cars on Campus (30) 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

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

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

模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<cstring> #include<stack> #include<vector> #include<iostream> using namesp

PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)

题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车连续多次进入,只取最后一个 2.如果一个车连续多次出去,只取第一个 3.一个车可能出入校园内好几次,停留时间取总和 实际上题目就是让我们求某个时间段内的车辆总和,时间段其实就相当于一个区间,区间求和的话,很快就联想到树状数组和线段树.然而怎么将时间段和区间联系起来呢,那就存储出现在记录和询问里的所有

1095. Cars on Campus (30)

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, t

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less tha

1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with follo