• PAT甲级——A1095 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


     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 #include <unordered_map>
     5 #include <algorithm>
     6 using namespace std;
     7 struct Node
     8 {
     9     string ID;
    10     bool flag;//1是进,0是出
    11     int time;
    12     Node(string s, bool f, int t) :ID(s), flag(f), time(t) {}
    13 };
    14 vector<Node>Cars;
    15 unordered_map<string, vector<Node>>map;//一辆车不止一个记录
    16 vector<string>res;
    17 int N, K, maxTime = 0;
    18 int main()
    19 {
    20     cin >> N >> K;
    21     string ID, flag;
    22     int h, m, s;
    23     while (N--)
    24     {    
    25         cin >> ID;
    26         scanf("%d:%d:%d", &h, &m, &s);
    27         cin >> flag;
    28         map[ID].push_back(Node(ID, flag == "in", h * 3600 + m * 60 + s));
    29     }
    30     for (auto ptr = map.begin(); ptr != map.end(); ++ptr)
    31     {
    32         sort(ptr->second.begin(), ptr->second.end(), [](Node a, Node b) {return a.time < b.time; });
    33         int t = 0;
    34         for (int i = 0; i < ptr->second.size()-1; ++i)
    35         {
    36             if (ptr->second[i].flag == 1 && ptr->second[i + 1].flag == 0)//一进一出
    37             {
    38                 Cars.push_back(ptr->second[i]);//记录合法时间
    39                 Cars.push_back(ptr->second[i + 1]);//记录合法时间
    40                 t += ptr->second[i + 1].time - ptr->second[i].time;//累加停车时间
    41             }
    42         }
    43         if (t > maxTime)
    44         {
    45             maxTime = t;
    46             res.clear();
    47             res.push_back(ptr->first);
    48         }
    49         else if(t==maxTime)
    50             res.push_back(ptr->first);
    51     }
    52     sort(Cars.begin(), Cars.end(), [](Node a, Node b) {return a.time < b.time; });
    53     int num = 0, i = 0;
    54     while (K--)
    55     {
    56         scanf("%d:%d:%d", &h, &m, &s);
    57         int time = h * 3600 + m * 60 + s;
    58         for (; i < Cars.size() && Cars[i].time <= time; ++i)
    59             num += Cars[i].flag ? 1 : -1;//使用用-1表示该车开出去了
    60         cout << num << endl;
    61     }
    62     sort(res.begin(), res.end());
    63     for (auto v : res)
    64         cout << v << " ";
    65     printf("%02d:%02d:%02d
    ", maxTime / 3600, maxTime % 3600 / 60, maxTime % 60);
    66     return 0;
    67 }
  • 相关阅读:
    strcpy 和 strnpy 区别
    js apply/call/caller/callee/bind使用方法与区别分析
    js中的this关键字详解
    Asp.Net_Get跟Post
    Asp.Net_Form验证跟授权
    Css_button样式对不齐
    Asp.Net_上传文件(ftp、webClient、webService)
    Asp.Net_优化
    Asp.Net_后台代码访问前台html标签
    Asp.Net_Ajax调用WebService返回Json前台获取循环解析
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11351942.html
Copyright © 2020-2023  润新知