Submission #928440

#TimeUsernameProblemLanguageResultExecution timeMemory
928440n1kLongest Trip (IOI23_longesttrip)C++17
15 / 100
9 ms856 KiB
#include "longesttrip.h"
#include <bits/stdc++.h>

using namespace std;

/*

OBSERVE:

path meargen

hinten und vorne dazu

* - * *

* - *

*/

std::vector<int> longest_trip(int N, int D){
    // vorraussetzung path[0].end nicht mit path[1].end verbunden
    vector<int> path[2];
    path[0].push_back(0);

    for(int i=1; i<N; i++){
        if(are_connected({*path[0].rbegin()}, {i})){
            path[0].push_back(i);
            if(path[1].size() and are_connected({path[0][path[0].size()-1]}, {path[1][path[1].size()-1]})){
                reverse(path[1].begin(), path[1].end());
                path[0].insert(path[0].end(), path[1].begin(), path[1].end());
                path[1].clear();
            }
        }else{
            path[1].push_back(i);
        }
    }

    if(path[1].empty() or not are_connected(path[0], path[1])){
        if(path[0].size() > path[1].size())
            return path[0];

        return path[1];
    }

    if(are_connected({path[0][0]}, {path[1][0]})){
        reverse(path[0].begin(), path[0].end());
        path[0].insert(path[0].end(), path[1].begin(), path[1].end());

        return path[0];
    }
    if(are_connected({path[0][0]}, {path[1][path[1].size()-1]})){
        path[1].insert(path[1].end(), path[0].begin(), path[0].end());

        return path[1];
    }
    if(are_connected({path[0][path[0].size()-1]}, {path[1][0]})){
        path[0].insert(path[0].end(), path[1].begin(), path[1].end());

        return path[0];
    }

    // jetzt wissen wir das path[0].begin - path[0].end
    // jetzt wissen wir das path[1].begin - path[1].end
    // und die pfade sind nicht disjoint

    // eine node in path[0] finden die mit path[1] verbunden ist
    // find i

    int li = 0, ri = path[0].size()-1;
    while(li<ri){
        int mi = (li + ri) / 2;
        vector<int> sub(path[0].begin(), path[0].begin()+mi+1);
        if(are_connected(sub, path[1])){
            ri = mi;
        }else{
            li = mi + 1;
        }
    }

    int i = li;

    int lj = 0, rj = path[1].size()-1;
    while(lj<rj){
        int mj = (lj + rj) / 2;
        vector<int> sub(path[1].begin(), path[1].begin()+mj+1);
        if(are_connected({i}, sub)){
            rj = mj;
        }else{
            lj = mj + 1;
        }
    }
    int j = lj;

    vector<int> ans;

    ans.insert(ans.end(), path[0].begin() + i + 1, path[0].end());
    ans.insert(ans.end(), path[0].begin(), path[0].begin() + i + 1);

    ans.insert(ans.end(), path[1].begin() + j, path[1].end());
    ans.insert(ans.end(), path[1].begin(), path[1].begin() + j);

    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...