Submission #911225

# Submission time Handle Problem Language Result Execution time Memory
911225 2024-01-18T16:18:43 Z biank Longest Trip (IOI23_longesttrip) C++17
5 / 100
11 ms 696 KB
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define SIZE(x) (int)x.size()
#define forn(i,n) for(int i=0;i<int(n);i++)
#define dforn(i,n) for(int i=int(n);i>=0;i--)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define pb push_back
typedef vector<int> vi;
bool are_connected(vi A, vi B);
int n;
const int MAXN = 256;
int c[MAXN][MAXN];

bool query(int x, int y) {
    if(c[x][y]==1) return true;
    if(c[x][y]==0) return false;
    forn(z,n) {
        if(z==x||z==y) continue;
        if(c[x][z] == 0 && c[y][z] == 0) {
            c[x][y]=c[y][x]=1;
            return true;
        }
    }
    c[x][y]=c[y][x]=are_connected({x},{y});
    return c[x][y]; 
}

int bs(vi a, vi b) {
    int l=0, r=SIZE(a);
    while(l+1<r) {
        int m=(l+r)/2;
        if(are_connected(b,vi(a.begin()+m,a.begin()+r))) l=m;
        else r=m;
    }
    return l;
}

vi longest_trip(int N, int D) {
    n=N;
    memset(c,-1,sizeof c);
    (void)D;
    vi P;
    forn(i,n) P.pb(i);
    srand(time(0));
    random_shuffle(ALL(P));
    vi a={P[0]}, b;
    forsn(i,1,n) {
        if(query(a.back(),P[i])) {
            a.pb(P[i]);
        } else if(!b.empty() && query(a.back(),b.back())) {
            dforn(j,SIZE(b)) a.pb(b[j]);
            b={P[i]};
        } else if(b.empty() || query(b.back(),P[i])) {
            b.pb(P[i]);
        }
    }
    if(SIZE(b)>SIZE(a)) swap(a,b);
    if(b.empty()||!are_connected(a,b)) return a;
    
    
    if(query(a.back(),b.front())) {
        forn(i,SIZE(b)) a.pb(b[i]);
        return a;
    } else if(query(b.back(),a.front())) {
        forn(i,SIZE(a)) b.pb(a[i]);
        return b;
    } else if(query(a.front(),b.front())) {
        reverse(ALL(a));
        forn(i,SIZE(b)) a.pb(b[i]);
        return a;
    } else if(query(a.back(),b.back())) {
        dforn(i,SIZE(b)) a.pb(b[i]);
        return a;
    }
    
    int i=bs(a,b), j=bs(b,{a[i]});
    
    vi ans;
    forsn(p,j+1,SIZE(b)) ans.pb(b[p]);
    forn(p,j+1) ans.pb(b[p]);
    forsn(p,i,SIZE(a)) ans.pb(a[p]);
    forn(p,i) ans.pb(a[p]);
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Incorrect 2 ms 696 KB invalid array
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 600 KB Output is correct
2 Correct 6 ms 600 KB Output is correct
3 Correct 5 ms 600 KB Output is correct
4 Correct 5 ms 680 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 600 KB Output is correct
2 Correct 7 ms 600 KB Output is correct
3 Correct 5 ms 600 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 6 ms 600 KB Output is correct
6 Correct 9 ms 600 KB Output is correct
7 Incorrect 1 ms 600 KB invalid array
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 600 KB Output is correct
2 Correct 6 ms 600 KB Output is correct
3 Correct 5 ms 600 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
6 Correct 9 ms 600 KB Output is correct
7 Incorrect 1 ms 600 KB invalid array
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 600 KB Output is correct
2 Correct 6 ms 600 KB Output is correct
3 Correct 5 ms 600 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
6 Correct 11 ms 600 KB Output is correct
7 Incorrect 1 ms 600 KB invalid array
8 Halted 0 ms 0 KB -