STL_1095 Cars on Campus (30 分)

1095 Cars on Campus

题目描述

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 inrecord. 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

 解题思路

首先按照车牌号进行排序,在车牌号相同的情况下,按照进出的顺序进行排序;在这样的顺序基础上,将错误的数据删除(只有in,或者只有out,连续两个in,删除第一个in,连续两个out,删除第二个out)

错误数据删除做法:两个指针,这两个指针始终相邻,只有当满足统一车牌号in和out的时候,将两个指针的数据放入新的vector中,两个指针向后移动两个单位;否则两个指针向后移动一个单位

然后将合理的数据按照进出时间排序,因为需要查找的时间是按照时间进行排序的,所以也只需要按照时间顺序判断车辆的进出即可,然后计算停留时间最长的车辆

程序

#include <iostream>
#include <initializer_list>
#include <algorithm>
#include <string>
#include <string.h>
#include <thread>
#include <cmath>
#include <vector>
#include <tuple>
#include <utility>
#include <type_traits>
#include <new>
#include <complex>
#include <queue>
#include <tuple>
#include <map>

using namespace std;
int n,k,max_time;
const int maxn = 1e4+5;
struct Record
{
    string num;
    int time;
    bool flag;
    bool operator <(const Record& a)const
    {
        if(num == a.num)
            return time < a.time;
        return num < a.num;
    }
};
vector<Record> rec,effect_rec;
vector<string> maxrec;
map<string,bool> mapp;

bool cmp(const Record& a,const Record& b)
{
    return a.time <b.time;
}
int get_time(int& time,int x){
    int tmp = time / x;
    time %= x;
    return tmp;
}
void get_maxrec()                     //计算停留时间最长的车辆
{
    max_time = 0;
    int now_time = 0;
    auto it = effect_rec.begin()+1,_front = effect_rec.begin();
    string last_num = _front->num;
    for(;it < effect_rec.end();)
    {
        if(it->num == last_num)
            now_time += it->time - _front->time;
        else
        {
            if(now_time > max_time)
                maxrec.clear(),max_time = now_time,maxrec.push_back(last_num);
            else if(now_time == max_time)
                maxrec.push_back(last_num);
            now_time = it->time - _front->time;
        }
        last_num = it->num;
        it += 2,_front += 2;
    }
    if(now_time > max_time)
        maxrec.clear(),max_time = now_time,maxrec.push_back(last_num);
    else if(now_time == max_time)
        maxrec.push_back(last_num);

}

int main()
{
    int hh,mm,ss,time;bool flag;
    string status,num;
    scanf("%d%d",&n,&k);
    for(int i = 0 ;i < n; i++)
    {
        cin >> num;
        scanf("%d:%d:%d",&hh,&mm,&ss);
        time = hh * 3600 + mm * 60 + ss;
        cin >> status;
        if(status == "in")
            flag = 1;
        else
            flag = 0;
        rec.emplace(rec.end(),Record{num,time,flag});
    }
    sort(rec.begin(),rec.end());
    auto _front = rec.begin();
    for(auto it = rec.begin()+1;it != rec.end();)        //排除错误数据
    {
        if(it->num == _front->num && it -> flag == 0 && _front ->flag == 1)
        {
            effect_rec.emplace(effect_rec.end(),*_front);
            effect_rec.emplace(effect_rec.end(),*it);
            if(it + 2 < rec.end())
                it += 2,_front += 2;
            else
                it++,_front++;
        }
        else
        {
            it ++,_front++;
        }
    }

    get_maxrec();
    sort(effect_rec.begin(),effect_rec.end(),cmp);
//    for_each(effect_rec.begin(),effect_rec.end(),[](const Record& a){
//        cout << a.num << " " << a.time << " " << a.flag <<endl;
//        });
    _front = effect_rec.begin();
    int ans = 0;
    while(k--)                                  //判断当前时刻校园内车辆的数量
    {
        scanf("%d:%d:%d",&hh,&mm,&ss);
        time = hh * 3600 + mm * 60 + ss;
        while(_front->time <= time)
        {
            if(mapp[_front->num] == 0 && _front -> flag == 1)
                ans ++,mapp[_front->num] = 1;
            else if(mapp[_front->num] == 1 && _front -> flag == 0)
                ans --,mapp[_front->num] = 0;
            _front ++;
        }
        cout << ans <<endl;
    }
    sort(maxrec.begin(),maxrec.end());                 //将停留时间最长的车辆排序
    auto _it = maxrec.begin();
    for(;_it != maxrec.end();_it ++)
        cout << *_it << " ";
    hh = get_time(max_time,3600);
    mm = get_time(max_time,60);
    printf("%02d:%02d:%02d",hh,mm,max_time);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值