zoukankan      html  css  js  c++  java
  • 1095 Cars on Campus (30分)(排序)

    1095 Cars on Campus (30分)

     

    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 <cstdio>
     2 #include <vector>
     3 #include <cstring>
     4 #include <map>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 const int maxn = 1e4 + 5;
     9 
    10 struct Record {
    11     char plate[10];
    12     int time, flag;
    13 } records[maxn];
    14 
    15 bool cmp(Record a, Record b) {
    16     if(strcmp(a.plate, b.plate)) {
    17         return strcmp(a.plate, b.plate) < 0;
    18     } else {
    19         return a.time < b.time;
    20     }
    21 }
    22 
    23 bool cmp1(Record a, Record b) {
    24     return a.time < b.time;
    25 }
    26 
    27 map <string, int> mp;
    28 int op[maxn * 8];
    29 
    30 vector <Record> vec;
    31 
    32 int Max;
    33 
    34 int main() {
    35     int n, k, h, m, s;
    36     char status[5];
    37     scanf("%d %d", &n, &k);
    38     for(int i = 0; i < n; i ++) {
    39         scanf("%s %d:%d:%d %s", records[i].plate, &h, &m, &s, status);
    40         records[i].flag = !strcmp(status, "in") ? 1 : -1;
    41         records[i].time = 3600 * h + 60 * m + s;
    42     }
    43     sort(records, records + n, cmp);
    44     for(int i = 0; i < n - 1; i ++) {
    45         if(!strcmp(records[i].plate, records[i + 1].plate) && records[i].flag == 1 && records[i + 1].flag == -1) {
    46             mp[records[i].plate] += records[i + 1].time - records[i].time;
    47             vec.push_back(records[i]);
    48             vec.push_back(records[i + 1]);
    49             Max = max(Max, mp[records[i].plate]);
    50         }
    51     }
    52     sort(vec.begin(), vec.end(), cmp1);
    53     vector <int> num(n);
    54     for(int i = 0; i < vec.size(); i ++) {
    55         if(i == 0) num[i] += vec[i].flag;
    56         else num[i] = num[i - 1] + vec[i].flag;
    57     }
    58     int idx = 0;
    59     for(int i = 0; i < k; i ++) {
    60         scanf("%d:%d:%d", &h, &m, &s);
    61         int time = h * 3600 + m * 60 + s;
    62         int j;
    63         for(j = idx; j < vec.size(); j ++) {
    64             if(vec[j].time > time) {
    65                 printf("%d
    ", num[j - 1]);
    66                 break;
    67             } else if(j == vec.size() - 1) {
    68                 printf("%d
    ", num[j]);
    69             }
    70         }
    71         idx = j;
    72     }
    73     map <string, int> :: iterator i;
    74     for(i = mp.begin(); i != mp.end(); i ++) {
    75         if(i -> second == Max) {
    76             printf("%s ", i -> first.c_str());
    77         }
    78     }
    79     printf("%02d:%02d:%02d
    ", Max / 3600, (Max % 3600) / 60,(Max % 3600) % 60);
    80     return 0;
    81 }


    这题我一开始搞复杂了,我发现自己现在写简单题很难一步到位想到最优解,根本原因我觉得是审题

    不仔细,有的时候写代码写着写着就把题目要注意的地方忘记了,我觉得以后写题,还是首先把题
    看清楚,然后把完整方案想出来,看有没有更简单的解,然后函数式解题,一步步实现,这样也会减少不必要的精力损耗。




  • 相关阅读:
    解析 AJAX 返回回来的 xml字符串
    JS 与 后台如何获取 Cookies
    鼠标上下滚轮事件
    MVC Control 返回各种数据
    ildasm 查看程序集 里面的图标的意思
    对象的序列化和反序列化 itprobie
    文件上传通用类 itprobie
    文件下载的四种方式 itprobie
    委托事件的实际运用 itprobie
    使用NPOI实现excel的导入导出 itprobie
  • 原文地址:https://www.cnblogs.com/bianjunting/p/13185392.html
Copyright © 2011-2022 走看看