Submission #846718

# Submission time Handle Problem Language Result Execution time Memory
846718 2023-09-08T09:52:19 Z IvanJ Longest Trip (IOI23_longesttrip) C++17
15 / 100
8 ms 600 KB
#include "longesttrip.h"
#include<bits/stdc++.h>

#define pb push_back
#define all(a) (a).begin(), (a).end()

using namespace std;

vector<int> longest_trip(int n, int D) {
    if(D == 3) {
        vector<int> p;
        for(int i = 0;i < n;i++) p.pb(i);
        return p;
    }
    if(D == 2) {
        deque<int> dq;
        if(!are_connected({0}, {1}))
            dq.pb(0), dq.pb(2), dq.pb(1);
        else if(!are_connected({1}, {2}))
            dq.pb(1), dq.pb(0), dq.pb(2);
        else dq.pb(0), dq.pb(1), dq.pb(2);

        for(int i = 3;i < n;i++) {
            if(are_connected({i}, {dq.front()}))
                dq.push_front(i);
            else dq.pb(i);
        }

        return vector<int>(all(dq));
    }
    if(D == 1) {
        vector<int> p1, p2;
        p1.pb(0);
        int flag = 0;
        for(int i = 1;i < n;i++) {
            int x = are_connected({i}, {p1.back()});
            if(!x) p2.pb(i);
            else {
                if(flag == 0) {
                    p1.pb(i), flag = 1;
                    reverse(all(p1));
                    reverse(all(p2));
                } else {
                    p1.pb(i), flag = 0;
                    if(p2.size() && are_connected({i}, {p2.back()})) {
                        while(p2.size())
                            p1.pb(p2.back()), p2.pop_back();
                    }
                    reverse(all(p1));
                    reverse(all(p2));
                }
            }
        }

        if((int)p1.size() == n) return p1;
        if(p1.size() < p2.size()) swap(p1, p2);
        if(!are_connected(p1, p2)) return p1;
        
        if(p2.size() == 1) {
            if(are_connected({p2[0]}, {p1[0]})) {
                reverse(all(p1));
                vector<int> p = p1;
                for(int i : p2) p.pb(i);
                return p;
            }
            if(are_connected({p2[0]}, {p1.back()})) {
                vector<int> p = p1;
                for(int i : p2) p.pb(i);
                return p;
            }
        } else {
            if(are_connected({p1[0], p1.back()}, {p2[0], p2.back()})) {
                if(are_connected({p1[0]}, {p2[0]})) {
                    reverse(all(p1));
                    vector<int> p = p1;
                    for(int i : p2) p.pb(i);
                    return p;
                } else if(are_connected({p1[0]}, {p2.back()})) {
                    vector<int> p = p2;
                    for(int i : p1) p.pb(i);
                    return p;
                } else if(are_connected({p1.back()}, {p2[0]})) {
                    vector<int> p = p1;
                    for(int i : p2) p.pb(i);
                    return p;
                } else {
                    reverse(all(p2));
                    vector<int> p = p1;
                    for(int i : p2) p.pb(i);
                    return p;
                }
            }
        }

        vector<int> v = p1;
        while(v.size() > 1) {
            int mid = (int)v.size() / 2;
            vector<int> v1, v2;
            for(int i = 0;i < mid;i++) v1.pb(v[i]);
            for(int i = mid;i < (int)v.size();i++) v2.pb(v[i]);
            if(are_connected(v1, p2)) v = v1;
            else v = v2;
        }

        int X = v[0];
        v = p2;
        while(v.size() > 1) {
            int mid = (int)v.size() / 2;
            vector<int> v1, v2;
            for(int i = 0;i < mid;i++) v1.pb(v[i]);
            for(int i = mid;i < (int)v.size();i++) v2.pb(v[i]);
            if(are_connected({X}, v1)) v = v1;
            else v = v2;
        }
        int Y = v[0];

        vector<int> p;
        int pos1 = -1, pos2 = -1;
        for(int i = 0;i < (int)p1.size();i++)
            if(p1[i] == X) pos1 = i;
        for(int i = 0;i < (int)p2.size();i++)
            if(p2[i] == Y) pos2 = i;

        for(int i = 0;i < (int)p1.size();i++)
            p.pb(p1[(i + pos1) % (int)p1.size()]);
        reverse(all(p));
        for(int i = 0;i < (int)p2.size();i++)
            p.pb(p2[(i + pos2) % (int)p2.size()]);
        return p;
    }
    return {};
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 344 KB Incorrect
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 600 KB Output is correct
11 Correct 4 ms 600 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 6 ms 600 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Incorrect 0 ms 344 KB Incorrect
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Incorrect 0 ms 344 KB Incorrect
17 Halted 0 ms 0 KB -