zoukankan      html  css  js  c++  java
  • PAT 1095 Cars on Campus

    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 (104​​), the number of records, and K (8×104​​) 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 accending 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

     题目大意:需要统计在特定的时间在校园内停车的总数,并且找出本天停车时间最长的那辆车。输入数据有n个车辆进出信息,并有m个时间点查询。

    每个车辆有车牌号 时间 状态,并且每个in都和下一个最近的out匹配,没有匹配的in或者out忽略掉。给定的查询时间是递增的。

    //将时间都转换为int存储。每来一个就查询一次时间复杂度好高啊,8次方了,千万级别。

    //但是如果有两个in,那么谁和第一个out对呢?那肯定是最后一个。

    //思路错了,如果有一辆车in out in out你这种方法就不行了。

    #include <iostream>
    #include <vector>
    #include <map>
    #include <string.h>
    #include<cstdio>
    using namespace std;
    struct Car{
        char name[10];
        int in,out;
        Car(char *n,int i,int o){
            name=n;in=i;out=o;//这样易于后边的排出。
        }
    };
    map<int,string> id2name;
    map<string,int> name2id;
    vector<Car> cars;
    int main() {
        int n,m;
        scanf("%d%d",&m,&n);
        char pl[10],state[5];
        int h,m,s;
        string nm;
        for(int i=0;i<n;i++){
            scanf("%s %d:%d:%d %s",&pl,&h,&m,&s,&state);
            nm=pl;
            int tm=h*3600+m*60+s;//总时间,从0开始计数。
            if(name2id.count(nm)==0&&state[0]=='i'){//如果以前没出现过,而且不是out直接pass即可。
                name2id[nm]=i;
                id2name[i]=nm;
                cars.push_back(Car(pl,tm,-1));
            }else if(name2id.count(nm)==1){//如果已经又过了
                
            }
        }
    
    
        return 0;
    }
    View Code

    //写到这里写不下去了,不知道该如何处理,如何判断。

     代码转自:https://www.liuchuo.net/archives/2951

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <map>
    using namespace std;
    struct node {
        char id[10];
        int time, flag = 0;
    };
    bool cmp1(node a, node b) {
        if(strcmp(a.id, b.id) != 0)
            return strcmp(a.id, b.id) < 0;//对所有的输入按照id排序。
        else
            return a.time < b.time;//同一辆车时间小的在前。
    }
    bool cmp2(node a, node b) {
        return a.time < b.time;
    }
    int main() {
        int n, k, maxtime = -1, tempindex = 0;
        scanf("%d%d
    ", &n, &k);
        vector<node> record(n), car;//将其确定大小,后序可以使用下标访问。
        for(int i = 0; i < n; i++) {
            char temp[5];
            int h, m, s;
            //输入这个字符串的时候不用再&符号了!
            scanf("%s %d:%d:%d %s
    ", record[i].id, &h, &m, &s, temp);
            int temptime = h * 3600 + m * 60 + s;
            record[i].time = temptime;
            record[i].flag = strcmp(temp, "in") == 0 ? 1 : -1;
        }
        sort(record.begin(), record.end(), cmp1);
        map<string, int> mapp;//id映射到时间。
        for(int i = 0; i < n - 1; i++) {
            if(strcmp(record[i].id, record[i+1].id) == 0 && record[i].flag == 1 && record[i+1].flag == -1) {
                car.push_back(record[i]);
                car.push_back(record[i+1]);
                mapp[record[i].id] += (record[i+1].time - record[i].time);
                if(maxtime < mapp[record[i].id]) {
                    maxtime = mapp[record[i].id];
                }
            }
        }
        sort(car.begin(), car.end(), cmp2);//再对car进行排序。
        vector<int> cnt(n);
        for(int i = 0; i < car.size(); i++) {
            if(i == 0)
                cnt[i] += car[i].flag;
             else
                cnt[i] = cnt[i - 1] + car[i].flag;
        }
        for(int i = 0; i < k; i++) {
            int h, m, s;
            scanf("%d:%d:%d", &h, &m, &s);
            int temptime = h * 3600 + m * 60 + s;
            int j;
            for(j = tempindex; j < car.size(); j++) {
                if(car[j].time > temptime) {//如果时间已经大于了当前,那么就不算。
                    printf("%d
    ", cnt[j - 1]);
                    break;
                } else if(j == car.size() - 1) {
                    printf("%d
    ", cnt[j]);
                }
            }
            tempindex = j;
        }
        for(map<string, int>::iterator it = mapp.begin(); it != mapp.end(); it++) {
            if(it->second == maxtime)
                printf("%s ", it->first.c_str());
        }
        printf("%02d:%02d:%02d", maxtime / 3600, (maxtime % 3600) / 60, maxtime % 60);
        return 0;
    }

    //这个cnt数组思路很好,学习了,需要复习啊,待会回想一下。

  • 相关阅读:
    线程阶段性总结——APM,ThreadPool,Task,TaskScheduler ,CancellationTokenSource
    研究BackgroundWorker后发现:AsyncOperation和SynchronizationContext的差异真的很大!
    线程同步——优势、劣势
    APM异步编程模型的优势
    DataGridView的VirtualMode,在大容量数据加载时特别有用
    【C】——C语言的位运算的优势
    【linux】——Linux tar打包命令
    【C】用文件和链表实现学生信息管理
    【C】——回调函数的好处
    【C】strcpy()需谨慎使用;
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9801488.html
Copyright © 2011-2022 走看看