Submission #436723

#TimeUsernameProblemLanguageResultExecution timeMemory
436723TangentRail (IOI14_rail)C++17
30 / 100
3065 ms218384 KiB
#include "rail.h"
#include "bits/stdc++.h"
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vii> vvii;
typedef vector<vll> vvll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
 
#define ffor(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define fford(i, a, b) for (ll i = (a); i > (ll)(b); i--)
#define rep(i, n) ffor(i, 0, n)
#define forin(x, a) for (auto &x: a)
#define all(a) a.begin(), a.end()

map<pii, int> memo;
int dist(int i, int j) {
    if (i > j) {
        swap(i, j);
    }
    if (!memo[{i, j}]) {
        memo[{i, j}] = getDistance(i, j);
    }
    return memo[{i, j}];
}

void findLocation(int N, int first, int location[], int stype[]) {
    set<pii> cs, ds;
    location[0] = first;
    stype[0] = 1;
    int closest = 1, mindist = dist(0, 1);
    ffor(i, 2, N) {
        if (dist(0, i) < mindist) {
            mindist = dist(0, i);
            closest = i;
        }
    }

    location[closest] = first + dist(0, closest);
    stype[closest] = 2;

    vii left, right;
    ffor(i, 1, N) {
        if (i != closest) {
            if (dist(0, i) == dist(0, closest) + dist(closest, i)) {
                left.emplace_back(i);
            } else {
                right.emplace_back(i);
            }
        }
    }
    
    function<void(vii&, int, bool)> search;
    search = [&](vii &curr, int bound, bool lbound) {
        if (curr.empty()) {
            return;
        }
        int closest2 = curr[0], mindist2 = dist(curr[0], bound);
        forin(i, curr) {
            if (dist(i, bound) < mindist2) {
                mindist2 = dist(i, bound);
                closest2 = i;
            }
        }

        if (lbound) {
            location[closest2] = location[bound] + dist(closest2, bound);
            stype[closest2] = 2;
            vii right2;
            int nbound = bound;
            forin(i, curr) {
                if (i != closest2) {
                    if (dist(bound, i) == dist(bound, closest2) + dist(closest2, i)) {
                        location[i] = location[closest2] - dist(closest2, i);
                        stype[i] = 1;
                        if (location[i] > location[nbound]) {
                            nbound = i;
                        }
                    } else {
                        right2.emplace_back(i);
                    }
                }
            }
            search(right2, nbound, true);
        } else {
            location[closest2] = location[bound] - dist(closest2, bound);
            stype[closest2] = 1;
            vii left2;
            int nbound = bound;
            forin(i, curr) {
                if (i != closest2) {
                    if (dist(bound, i) == dist(bound, closest2) + dist(closest2, i)) {
                        location[i] = location[closest2] + dist(closest2, i);
                        stype[i] = 2;
                        if (location[i] < location[nbound]) {
                            nbound = i;
                        }
                    } else {
                        left2.emplace_back(i);
                    }
                }
            }
            search(left2, nbound, false);
        }
    };

    search(left, closest, false);
    search(right, 0, true);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...