zoukankan      html  css  js  c++  java
  • 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 (≤), the number of records, and K (≤) 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

    题意:

    车辆有进有出,根据车辆进出的时间,来回答每一个时间校园内的车辆数。以及哪一个停车位内的车辆停留的时间最长。

    注意:

    每一个停车位内一段时间只能停一辆车(姑且认为是停车位,这样好理解点),也就是说记录,必须是一进一出成对出现,若连续两辆车都是进入同一个停车位,则先进的那个忽略不计。同理,若两辆车连续开出同一个停车位,则后开出去的那个忽略不计。

    根据样例可知,period应该是前闭后开,即在某一时间点统计车辆个数的时候,如果统计的时间点与进来的时间点相同,则将其计入在内,如果统计的时间点与出去的时间点相同则不将其计入在内。

    Code:

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<map>
    #include<iomanip>
    #include<stdio.h>
    
    using namespace std;
    
    struct Record {
        string plate_name;
        int time;
        string status;
        Record(string s1, int t, string s2): plate_name(s1), time(t), status(s2) {}
    };
    
    struct Plate {
        string name;
        int num = 0;
        int period = 0;
        int startTime = 0;
    };
    
    bool cmp1(Record a, Record b) {
        return a.time < b.time;
    }
    
    bool cmp2(pair<int, string> a, pair<int, string> b) {
        return a.first > b.first;
    }
    
    int toSecond(string time) {
        string hh, mm, ss;
        hh = time.substr(0, 2);
        mm = time.substr(3, 2);
        ss = time.substr(6, 2);
        return stoi(hh)*60*60 + stoi(mm)*60 + stoi(ss);
    }
    
    int main() {
        int n, m;
        scanf("%d%d", &n, &m);
    
        vector<Record> v;
        map<string, Plate> mp;
        string plate_name, time, status;
        for (int i = 0; i < n; ++i) {
            cin >> plate_name >> time >> status;
            int sec = toSecond(time);
            if (mp.find(plate_name) == mp.end()) mp[plate_name] = {plate_name, 0, 0, 0};
            Record r = Record(plate_name, sec, status);
            v.push_back(r);
        }
        sort(v.begin(), v.end(), cmp1);
        int len = v.size();
        vector<pair<int, int> > per;
        for (int i = 0; i < len; ++i) {
            string name = v[i].plate_name;
            if (v[i].status == "in") {
                if (mp[name].num > 0) {
                    mp[name].startTime = v[i].time;
                } else {
                    mp[name].num = 1;
                    mp[name].startTime = v[i].time;
                }
            } else {
                if (mp[name].num > 0) {
                    mp[name].num = 0;
                    mp[name].period += v[i].time - mp[name].startTime;
                    per.push_back({mp[name].startTime, v[i].time});
                } else {
                    continue;
                }
            }
        }
    
        int count;
        for (int i = 0; i < m; ++i) {
            string query;
            cin >> query;
            count = 0;
            int t = toSecond(query);
            for (int j = 0; j < per.size(); ++j) {
                if (t < per[j].second && t >= per[j].first) count++;
            }
            printf("%d
    ", count);
        }
    
        vector<pair<int, string> > v2;
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            int per = it->second.period;
            string str = it->second.name;
            v2.push_back({per, str});
        }
        
        sort(v2.begin(), v2.end(), cmp2);
    
        printf("%s ", v2[0].second.c_str());
        for (int i = 1; i < v2.size(); ++i) {
            if (v2[i].first == v2[i-1].first)
                printf("%s ", v2[i].second.c_str());
        }
        int max_time = v2[0].first;
        int hh = max_time / 3600;
        int mm = (max_time % 3600) / 60;
        int ss = (max_time % 3600) % 60;
    
        printf("%02d:%02d:%02d
    ", hh, mm, ss);
    
        return 0;
    }
    

      

    写了半天,最后发现还是又通过不了的样例。代码还得再优化。

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    jquery1.9以上版本如何使用toggle函数
    oracle分区表知识
    oracle删除表以及清理表空间
    oracle查询单表占用空间的大小
    CSS样式案例(2)-制作一个简单的登录界面
    oracle数据库启动
    创业项目
    dataguru试听课程
    从机器学习谈起
    your project contains error(s),please fix them before running your application.错误总结
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12616354.html
Copyright © 2011-2022 走看看