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\n", 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\n", 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\n", 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\n", max/3600, max%3600/60, max%60);
74 return 0;}

猜你喜欢

转载自www.cnblogs.com/mr-stn/p/9183842.html