美文网首页
1095 Cars on Campus

1095 Cars on Campus

作者: 100PER | 来源:发表于2020-03-08 00:31 被阅读0次

    题目:Cars on Campus

    思路:两次排序:第一次通过车牌排序,然后判断数据是否合法,并找出最长的停车时间;第二次通过时间排序,按时间统计出车辆数。

    代码:

    #include<cstdio>
    #include<string>
    #include<vector>
    #include<iostream>
    #include<algorithm>
    #include<map>
    using namespace std;
    class CAR{
        public:
            string Pnum;
            int time;
            int status;
            bool isValid;
            CAR(string num1,int time1,int status1){
                Pnum = num1;
                time = time1;
                status = status1;
                isValid = false;
            }
    };
    
    int main(){
        int N, K;
        scanf("%d %d", &N, &K);
        vector<CAR> parking;
        string t_pnum;
        int t_hour, t_min, t_sec;
        string t_status;
        for (int i = 0; i < N;i++){
            cin >> t_pnum;
            scanf("%d:%d:%d", &t_hour, &t_min, &t_sec);
            int tempt = t_hour * 3600 + t_min * 60 + t_sec;
            cin >> t_status;
            int temps = t_status[0] == 'i' ? 0 : 1;
            parking.emplace_back(t_pnum, tempt, temps);
        }
        sort(parking.begin(), parking.end(), [](CAR x, CAR y){
            if(x.Pnum.compare(y.Pnum)==0){
                return x.time < y.time;
            }else{
                return x.Pnum.compare(y.Pnum)<0;
            }
        });
        int maxlength = 0;
        map<string, int> lenth;
        vector<string> longest;
        for (int i = 0; i+1 < parking.size();++i){
            if(parking[i].status == 0){
                if(parking[i+1].status == 1 && parking[i].Pnum.compare(parking[i+1].Pnum) == 0){
                    auto it = lenth.find(parking[i].Pnum);
                    if(it!=lenth.end())
                    {
                        it->second += (parking[i + 1].time - parking[i].time);
                    }else{
                        lenth[parking[i].Pnum] = (parking[i + 1].time - parking[i].time);
                    }
                    if(lenth[parking[i].Pnum] > maxlength){
                        maxlength = lenth[parking[i].Pnum];
                    }
                    parking[i].isValid = parking[i + 1].isValid = true;
                }
            }
        }
        for (auto it = lenth.begin(); it != lenth.end();++it){
            if(it->second == maxlength){
                longest.push_back(it->first);
            }
        }
        sort(longest.begin(), longest.end(), [](string x, string y) {
            return x.compare(y) < 0;
        });
        sort(parking.begin(), parking.end(), [](CAR x, CAR y) {
            if(x.time == y.time){
                return x.Pnum.compare(y.Pnum) < 0;
            }else{
                return x.time < y.time;
            }
        });
        int j = 0;
        int numCar = 0;
        for (int i = 0; i < K;i++){
            int hh, mm, ss;
            scanf("%d:%d:%d", &hh, &mm, &ss);
            int temp = hh * 3600 + mm * 60 + ss;
            for (; j<parking.size() && temp >= parking[j].time;j++){
                if(parking[j].isValid){
                    if(parking[j].status == 0){
                        numCar++;
                    }else{
                        numCar--;
                    }
                }
            }
            cout << numCar << endl;
        }
    
        // for (auto it = parking.begin(); it != parking.end();it++){
        //     cout << it->Pnum << " " << it->status << " " << it->time << " " << it->isValid<<endl;
        // }
         for (auto it = longest.begin(); it != longest.end();it++){
             cout << *it << " ";
         }
         int hh, mm, ss;
         hh = maxlength / 3600;
         maxlength %= 3600;
         mm = maxlength / 60;
         maxlength %= 60;
         ss = maxlength;
         printf("%02d:%02d:%02d", hh, mm, ss);
         return 0;
    }
    

    C++语法复习

    • vector
      • emplace_back() 添加,需要构造函数
    • map
      • find() 找不到返回 .end()
      • map迭代器 it->first是key,it->second是value
    • lambda

    相关文章

      网友评论

          本文标题:1095 Cars on Campus

          本文链接:https://www.haomeiwen.com/subject/xaoxhhtx.html