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

    Zhejiang University has 6 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 (<= 10000), the number of records, and K (<= 80000) 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 ascendingorder 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.建立结构node,保存车辆的车牌id,进入或出的时间time, 进出的标志flag,为0的时候,表示进, 反之表示出
        2.录入车辆的信息,对车辆排序,按照 车牌优先排序 时间次排序 的规则进行排序,把相同车辆的信息按照时间先后排在一起, 从而可以筛选出合格的信息, 把合格的保存到另外一个数组中
        3.在将所有合格的信息按照时间先后进行排序, 因为给出的查询时间都是升序的,所以可以对车辆信息进行顺序查找不导致超时;
        4.查询停车时间最长的, 在筛选合格信息的时候,用map<string, int> time,记录下不同车辆的停车时间; 查找最长停车时间也是一件比较繁琐的事情, 受限于map的性质,先遍历map找到最大值,再次遍历map找到具有最大值的车辆
    注意点:对于数组访问,以及要修改数组指针的,需要特别注意修改指针与访问数组的先后顺序
        在查询的时候可能后面几个查询时间均大于最后一辆车离开的时间,所以在循环中找不到符合条件的时间,导致没有输出,应该在查找循环中添加一个标志位,标志是否找到,若没有找到,做额外处理
    因为这两个点花了不少时间找bug,在程序代码稍微长点的时候,就很容易犯错; 慢慢积累吧;


    有一个测试点超时,把输出全部改为printf就行;
    #include<iostream>
    #include<map>
    #include<vector>
    #include<algorithm>
    #include<string>
    using namespace std;
    struct node{
        string id;
        int time;
        bool flag;
    };
    
    bool cmp1(node a, node b){
        if(a.id!=b.id) return a.id>b.id;
        return a.time<b.time;
    }
    
    bool cmp2(node a, node b){return a.time<b.time;}
    
    int main(){
        int n, k, i, j, hr, min, sec, t;
        vector<node> v1, v2;
        map<string, int> time;
        cin>>n>>k;
        string s1, s2;
        for(i=0; i<n; i++){
            cin>>s1;
            scanf("%d:%d:%d", &hr, &min, &sec);
            cin>>s2;
            t = hr*3600 + min*60 + sec;
            node nnode;
            nnode.id=s1; nnode.time=t; nnode.flag = (s2[0]=='i') ? 0 : 1;
            v1.push_back(nnode);
        }
        sort(v1.begin(), v1.end(), cmp1);
        for(i=0; i<n-1;){
            if(v1[i].flag==0 && v1[i+1].flag==1 && v1[i].id==v1[i+1].id){
                v2.push_back(v1[i]);
                v2.push_back(v1[i+1]);
                time[v1[i].id] += (v1[i+1].time-v1[i].time);
                i += 2;
            }else i++;
        }
        sort(v2.begin(), v2.end(), cmp2);
        int begin=0, sum=0;
        for(i=0; i<k; i++){
            scanf("%d:%d:%d", &hr, &min, &sec);
            t = 3600*hr + 60*min + sec;
            bool flag=true;
            for(j=begin; j<v2.size(); j++){
                if(t<v2[j].time){
                    cout<<sum<<endl;
                    begin=j;
                    flag=false;
                    break;
                }
                if(v2[j].flag==0) sum++;
                else sum--;
            }
            if(flag) cout<<sum<<endl;
        }
        map<string, int>::iterator it;
        int max=0;
        for(it=time.begin(); it!=time.end(); it++){
            if(it->second > max) max = it->second;
        }
        vector<string> temp;
        for(it=time.begin(); it!=time.end(); it++){
            if(it->second==max) temp.push_back(it->first); 
        }
        sort(temp.begin(), temp.end());
        for(i=0; i<temp.size(); i++) cout<<temp[i]<<" ";
        printf("%02d:%02d:%02d
    ", max/3600, max%3600/60, max%60);
    return 0;}

    修改过后的代码

     1 #include<iostream>
     2 #include<map>
     3 #include<vector>
     4 #include<algorithm>
     5 #include<string>
     6 using namespace std;
     7 struct node{
     8     string id;
     9     int time;
    10     bool flag;
    11 };
    12 
    13 bool cmp1(node a, node b){
    14     if(a.id!=b.id) return a.id>b.id;
    15     return a.time<b.time;
    16 }
    17 
    18 bool cmp2(node a, node b){return a.time<b.time;}
    19 
    20 int main(){
    21     int n, k, i, j, hr, min, sec, t;
    22     vector<node> v1, v2;
    23     map<string, int> time;
    24     cin>>n>>k;
    25     string s1;
    26     char s2[5];
    27     for(i=0; i<n; i++){
    28         cin>>s1;
    29         scanf("%d:%d:%d %s", &hr, &min, &sec, s2);
    30         t = hr*3600 + min*60 + sec;
    31         node nnode;
    32         nnode.id=s1; nnode.time=t; nnode.flag = (s2[0]=='i') ? 0 : 1;
    33         v1.push_back(nnode);
    34     }
    35     sort(v1.begin(), v1.end(), cmp1);
    36     for(i=0; i<n-1;){
    37         if(v1[i].flag==0 && v1[i+1].flag==1 && v1[i].id==v1[i+1].id){
    38             v2.push_back(v1[i]);
    39             v2.push_back(v1[i+1]);
    40             time[v1[i].id] += (v1[i+1].time-v1[i].time);
    41             i += 2;
    42         }else i++;
    43     }
    44     sort(v2.begin(), v2.end(), cmp2);
    45     int begin=0, sum=0;
    46     for(i=0; i<k; i++){
    47         scanf("%d:%d:%d", &hr, &min, &sec);
    48         t = 3600*hr + 60*min + sec;
    49         bool flag=true;
    50         for(j=begin; j<v2.size(); j++){
    51             if(t<v2[j].time){
    52                 printf("%d
    ", sum);
    53                 begin=j;
    54                 flag=false;
    55                 break;
    56             }
    57             if(v2[j].flag==0) sum++;
    58             else sum--;
    59         }
    60         if(flag) printf("%d
    ", sum); 
    61     }
    62     map<string, int>::iterator it;
    63     int max=0;
    64     for(it=time.begin(); it!=time.end(); it++){
    65         if(it->second > max) max = it->second;
    66     }
    67     vector<string> temp;
    68     for(it=time.begin(); it!=time.end(); it++){
    69         if(it->second==max) temp.push_back(it->first); 
    70     }
    71     sort(temp.begin(), temp.end());
    72     for(i=0; i<temp.size(); i++) printf("%s ", temp[i].c_str());
    73     printf("%02d:%02d:%02d
    ", max/3600, max%3600/60, max%60);
    74 return 0;}

    再次修改之后的代码, 对车辆信息的的序号进行排序, 避免了大量数据的移动

     1 #include<iostream>
     2 #include<map>
     3 #include<vector>
     4 #include<algorithm>
     5 #include<string>
     6 using namespace std;
     7 struct Node{
     8   string plate;
     9   int time, status;
    10   int hour, min, sec;
    11   int cnt;
    12 };
    13 vector<Node> v;
    14 bool cmp(int a, int b){
    15   if(v[a].plate!=v[b].plate) return v[a].plate<v[b].plate;
    16   return v[a].time<v[b].time;
    17 }
    18 bool cmp1(int a, int b){ return v[a].time<v[b].time;}
    19 
    20 int main(){
    21   int n, k, i;
    22   cin>>n>>k;
    23   v.resize(n);
    24   vector<int> idx(n);
    25   for(i=0; i<n; i++){
    26       idx[i]=i;
    27     string status;
    28     cin>>v[i].plate;
    29     scanf("%d:%d:%d", &v[i].hour, &v[i].min, &v[i].sec);
    30     cin>>status;
    31     v[i].time = v[i].hour*3600+60*v[i].min+v[i].sec;
    32     v[i].status = status[0]=='i' ? 1 : 0;
    33     v[i].cnt=1;
    34   }
    35   sort(idx.begin(), idx.end(), cmp);
    36   map<string, int> timeCnt;
    37   vector<int> valid;
    38   for(i=0; i<n-1; ){
    39     int idx1=idx[i], idx2=idx[i+1];
    40     if(v[idx1].plate==v[idx2].plate && v[idx1].status-v[idx2].status==1){
    41       valid.push_back(idx1);
    42       valid.push_back(idx2);
    43       timeCnt[v[idx1].plate] += (v[idx2].time - v[idx1].time);
    44       i += 2;
    45     }else i++;
    46   }
    47   
    48   sort(valid.begin(), valid.end(), cmp1);
    49   
    50   for(i=1; i<valid.size(); i++){
    51     int index=valid[i], lastIndex=valid[i-1];
    52     if(v[index].status==1) v[index].cnt = v[lastIndex].cnt+1;
    53     else v[index].cnt = v[lastIndex].cnt-1;
    54   }
    55   int j=0;
    56   for(i=0; i<k; i++){
    57     int h, m, s, total;
    58     scanf("%d:%d:%d", &h, &m, &s);
    59     total = h*3600+60*m+s;
    60     int len=valid.size();
    61     for(; j<len-1; j++){
    62       int idx1=valid[j], idx2=valid[j+1];
    63       if(v[idx1].time<=total && v[idx2].time>total){
    64         cout<<v[idx1].cnt<<endl;
    65         break;
    66       }
    67     }
    68     if(total>=v[valid[len-1]].time) cout<<v[valid[len-1]].cnt<<endl;
    69   }
    70 
    71   vector<string> ans;
    72   int maxn=-1;
    73   map<string, int>::iterator it = timeCnt.begin();
    74   for(; it!=timeCnt.end(); it++){
    75     if(it->second > maxn){
    76       ans.clear();
    77       ans.push_back(it->first);
    78       maxn = it->second;
    79     }else if(it->second == maxn) ans.push_back(it->first);
    80   }
    81   for(i=0; i<ans.size(); i++) cout<<ans[i]<<" ";
    82   printf("%02d:%02d:%02d
    ", maxn/3600, maxn%3600/60, maxn%60);
    83   return 0;
    84 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    ios 手写键盘闪退问题 UIKBBlurredKeyView candidateList
    ios NSURLErrorDomain Code=-1004 "未能连接到服务器。问题
    最牛B的编码套路
    watchOS开发—初步认识
    iOS开发拓展篇—蓝牙之CoreBlueTooth(BLE)
    iOS开发拓展篇—蓝牙之mutipeerConnectivity的使用
    iOS开发拓展篇—蓝牙之GameKit使用
    iOS开发拓展篇—ReactiveCocoa常见操作方法介绍(进阶篇)
    iOS开发拓展篇—ReactiveCocoa介绍(基础篇)
    iOS开发拓展篇—异常处理
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9183842.html
Copyright © 2011-2022 走看看