제출 #700691

#제출 시각아이디문제언어결과실행 시간메모리
700691Cyanmond철로 (IOI14_rail)C++17
100 / 100
297 ms98896 KiB
#include "rail.h"
#include <bits/stdc++.h>

void findLocation(int N, int first, int location[], int stype[]) {
    stype[0] = 1;
    location[0] = first;
    if (N == 1) {
        return;
    }
    std::vector<std::vector<int>> dists(N, std::vector<int>(N, -1));
    auto dist = [&](const int a, const int b) {
        assert(0 <= a and 0 <= b and a < N and b < N);
        if (a == b) {
            return 0;
        }
        if (dists[a][b] != -1) {
            return dists[a][b];
        }
        return dists[a][b] = dists[b][a] = getDistance(a, b);
    };

    std::vector<int> lkOrder(N);
    std::iota(lkOrder.begin(), lkOrder.end(), 0);
    std::sort(lkOrder.begin(), lkOrder.end(), [&](const int a, const int b) {
        return dist(0, a) < dist(0, b);
    });
    assert(lkOrder[0] == 0);
    const int sR = lkOrder[1];
    stype[sR] = 2;
    location[sR] = location[0] + dist(0, sR);

    std::vector<int> dRights, cLefts;
    std::unordered_set<int> used;
    used.insert(location[0]);
    used.insert(location[sR]);
    dRights.push_back(sR);
    bool fin = false;

    for (int i = 2; i < N; ++i) {
        const int v = lkOrder[i];
        bool isLeft = dist(0, v) == (dist(sR, v) + dist(0, sR));
        if (isLeft) {
            const int d = dist(sR, v);
            if (d < dist(0, sR)) {
                cLefts.push_back(v);
                location[v] = location[sR] - d;
                stype[v] = 1;
                used.insert(location[v]);
                continue;
            } else if (not fin) {
                cLefts.push_back(0);
                fin = true;
            }
            std::vector<int> pos;
            for (const int p : cLefts) {
                const int posA = location[p];
                const int posB = posA + (d - dist(sR, p));
                if (posB < location[0] and used.find(posB) == used.end()) {
                    pos.push_back(posB);
                }
            }

            const int b = cLefts.back();
            const int f1 = location[b] + dist(b, v);
            if (std::find(pos.begin(), pos.end(), f1) == pos.end()) {
                location[v] = location[sR] - d;
                stype[v] = 1;
                cLefts.push_back(v);
                used.insert(location[v]);
            } else {
                location[v] = f1;
                stype[v] = 2;
                used.insert(location[v]);
            }
        } else {
            const int d = dist(0, v);
            std::vector<int> pos;
            for (const int p : dRights) {
                const int posA = location[p];
                const int posB = posA - (d - dist(0, p));
                if (posB > location[0] and used.find(posB) == used.end()) {
                    pos.push_back(posB);
                }
            }
            
            const int b = dRights.back();
            const int f1 = location[b] - dist(b, v);
            if (std::find(pos.begin(), pos.end(), f1) == pos.end()) {
                location[v] = location[0] + d;
                stype[v] = 2;
                dRights.push_back(v);
                used.insert(location[v]);
            } else {
                location[v] = f1;
                stype[v] = 1;
                used.insert(location[v]);
            }
        }
    }
    /*
    for (int i = 0; i < N; ++i) {
        std::cout << stype[i] << ' ' << location[i] << std::endl;
    }
    */
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...