답안 #932689

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932689 2024-02-24T03:13:58 Z Programmer123 분수 공원 (IOI21_parks) C++17
15 / 100
3500 ms 134024 KB
#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,sse4.2")
#endif

#include <bits/stdc++.h>
#include "parks.h"

std::unordered_map<int, int> map[100001];
int N;
std::unordered_map<int, std::vector<int>> rely[100002];
std::vector<std::pair<int, int>> vld[200000];
std::pair<int, int> assigned[200000];
std::unordered_set<int> taken[100002];
time_t stop;
std::default_random_engine Rand(std::random_device{}());

bool shuffle = true;
bool Shuffle = false;

bool doomed(int x) {
    for (auto a: vld[x]) if (!taken[a.first / 2].count(a.second)) return false;
    return true;
}

bool cantake(std::pair<int, int> a, int n) {
    for (auto x: rely[a.first / 2][a.second]) if (x > n && doomed(x)) return false;
    return true;
}

#ifdef LOCAL
size_t called = 0;
#endif

bool backtrack(int n) {
#ifdef LOCAL
    called++;
#endif
    if (n == N - 1) return true;
    if (clock() > stop) return false;
//    if (Shuffle && vld[n].size() == 2 && Rand() % 2) {
//        std::swap(vld[n][0], vld[n][1]);
//    }
    for (auto a: vld[n]) {
        if (taken[a.first / 2].count(a.second)) continue;
        taken[a.first / 2].insert(a.second);
        if (!cantake(a, n)) {
            taken[a.first / 2].erase(a.second);
            continue;
        }
        if (backtrack(n + 1)) {
            assigned[n] = a;
            return true;
        }
        taken[a.first / 2].erase(a.second);
    }
    return false;
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
    stop = clock() + 3.4 * CLOCKS_PER_SEC;
    N = (int) x.size();
    if (N == 1) {
        build({}, {}, {}, {});
        return 1;
    }
    int maxx = x[0];
    int minx = x[0];
    int maxy = y[0];
    int miny = y[0];
    for (int i = 0; i < N; ++i) {
        maxx = std::max(maxx, x[i]);
        minx = std::min(minx, x[i]);
        maxy = std::max(maxy, y[i]);
        miny = std::min(miny, y[i]);
    }
    for (int i = 0; i < N; ++i) {
        map[x[i] / 2][y[i]] = i;
    }
    while (true) {
        std::vector<int> u, v, a, b;
        bool seen[N];
        for (int i = 0; i < N; ++i) {
            seen[i] = false;
        }
        std::queue<int> bfs;
        int start = 0;
        for (int i = 0; i < N; ++i) {
            if (x[i] < x[start]) start = i;
            if (x[i] == x[start] && y[i] < y[start]) start = i;
        }
        if (Shuffle) {
            start = Rand() % N;
        }
        bfs.push(start);
        seen[start] = true;
        auto add = [&](int x, int y, std::vector<int> &data) {
            if (x < 0 || x > 200000) return;
            if (map[x / 2].count(y)) data.push_back(map[x / 2][y]);
        };
        auto edges = [&](int x, int y) {
            std::vector<int> result;
            add(x, y + 2, result);
            add(x, y - 2, result);
            add(x + 2, y, result);
            add(x - 2, y, result);
            if (Shuffle) {
                std::shuffle(result.begin(), result.end(), Rand);
            }
            return result;
        };
        int rem = N - 1;
        while (!bfs.empty()) {
            auto next = bfs.front();
            bfs.pop();
            for (auto n: edges(x[next], y[next])) {
                if (!seen[n]) {
                    seen[n] = true;
                    bfs.push(n);
                    rem--;
                    u.push_back(next);
                    v.push_back(n);
                }
            }
        }
        if (rem)
            return 0;

        std::function<std::vector<std::pair<int, int>>(int, int)> valid = [&x, &y, minx, maxx, miny, maxy](int u,
                                                                                                           int v) -> std::vector<std::pair<int, int>> {
            if (x[u] == x[v]) {
                int _y = (y[u] + y[v]) / 2;
                if (x[u] == minx) return {{minx - 1, _y}};
                if (x[u] == maxx) return {{maxx + 1, _y}};
                return {{x[u] + 1, _y},
                        {x[u] - 1, _y}};
            } else {
                int _x = (x[u] + x[v]) / 2;
                if (y[u] == miny) return {{_x, miny - 1}};
                if (y[u] == maxy) return {{_x, maxy + 1}};
                return {{_x, y[u] + 1},
                        {_x, y[u] - 1}};
            }
        };
        if (shuffle) {
            std::vector<std::pair<int, int>> Edges;
            for (int i = 0; i < N - 1; ++i) {
                Edges.emplace_back(u[i], v[i]);
            }
            std::sort(Edges.begin(), Edges.end(), [&](std::pair<int, int> a, std::pair<int, int> b) {
                int A = a.first;
                int B = b.first;
                if (x[A] == x[B]) return y[A] < y[B];
                return x[A] < x[B];
            });
            for (int i = 0; i < N - 1; ++i) {
                u[i] = Edges[i].first;
                v[i] = Edges[i].second;
            }
        }
        for (int i = 0; i < N - 1; ++i) {
            vld[i] = valid(u[i], v[i]);
            if (Shuffle && vld[i].size() == 2 && Rand() % 2) {
                std::swap(vld[i][0], vld[i][1]);
            }
        }
        for (int i = 0; i < N - 1; ++i) {
            for (auto n: vld[i]) {
                rely[n.first / 2][n.second].push_back(i);
            }
        }
        if (backtrack(0)) {
#ifdef LOCAL
            printf("Called backtrack %ld times.\n", called);
            called = 0;
#endif
            for (int i = 0; i < N - 1; ++i) {
                a.push_back(assigned[i].first);
                b.push_back(assigned[i].second);
            }
            build(u, v, a, b);
            return 1;
        }
#ifdef LOCAL
        printf("Called backtrack %ld times.\n", called);
        called = 0;
#endif
        for (auto _x: x) {
            rely[(_x - 1) / 2].clear();
            rely[(_x + 1) / 2].clear();
        }
        Shuffle = shuffle;
        shuffle = true;
        if (clock() > stop) return 0;
#ifdef LOCAL
        printf("Retrying...\n");
#endif
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 7 ms 23132 KB Output is correct
4 Correct 7 ms 23128 KB Output is correct
5 Correct 6 ms 23132 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 23132 KB Output is correct
8 Correct 7 ms 23132 KB Output is correct
9 Correct 146 ms 66548 KB Output is correct
10 Correct 22 ms 27188 KB Output is correct
11 Correct 72 ms 46192 KB Output is correct
12 Correct 24 ms 29476 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 9 ms 23132 KB Output is correct
16 Correct 160 ms 66520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 7 ms 23132 KB Output is correct
4 Correct 7 ms 23128 KB Output is correct
5 Correct 6 ms 23132 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 23132 KB Output is correct
8 Correct 7 ms 23132 KB Output is correct
9 Correct 146 ms 66548 KB Output is correct
10 Correct 22 ms 27188 KB Output is correct
11 Correct 72 ms 46192 KB Output is correct
12 Correct 24 ms 29476 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 9 ms 23132 KB Output is correct
16 Correct 160 ms 66520 KB Output is correct
17 Correct 6 ms 23128 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 7 ms 23128 KB Output is correct
20 Correct 7 ms 23112 KB Output is correct
21 Correct 7 ms 23132 KB Output is correct
22 Correct 7 ms 23384 KB Output is correct
23 Correct 316 ms 108540 KB Output is correct
24 Correct 6 ms 23132 KB Output is correct
25 Correct 8 ms 23644 KB Output is correct
26 Correct 8 ms 23132 KB Output is correct
27 Correct 7 ms 23132 KB Output is correct
28 Correct 120 ms 55556 KB Output is correct
29 Correct 190 ms 74096 KB Output is correct
30 Correct 238 ms 88364 KB Output is correct
31 Correct 327 ms 108476 KB Output is correct
32 Correct 7 ms 23128 KB Output is correct
33 Correct 7 ms 23128 KB Output is correct
34 Correct 7 ms 22876 KB Output is correct
35 Correct 8 ms 23132 KB Output is correct
36 Correct 7 ms 23132 KB Output is correct
37 Correct 6 ms 22956 KB Output is correct
38 Correct 6 ms 23132 KB Output is correct
39 Correct 7 ms 23132 KB Output is correct
40 Correct 7 ms 23120 KB Output is correct
41 Correct 7 ms 22972 KB Output is correct
42 Correct 6 ms 23132 KB Output is correct
43 Correct 7 ms 23136 KB Output is correct
44 Correct 8 ms 23132 KB Output is correct
45 Correct 164 ms 68188 KB Output is correct
46 Correct 249 ms 88776 KB Output is correct
47 Correct 257 ms 88720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 7 ms 23132 KB Output is correct
4 Correct 7 ms 23128 KB Output is correct
5 Correct 6 ms 23132 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 23132 KB Output is correct
8 Correct 7 ms 23132 KB Output is correct
9 Correct 146 ms 66548 KB Output is correct
10 Correct 22 ms 27188 KB Output is correct
11 Correct 72 ms 46192 KB Output is correct
12 Correct 24 ms 29476 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 9 ms 23132 KB Output is correct
16 Correct 160 ms 66520 KB Output is correct
17 Correct 6 ms 23128 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 7 ms 23128 KB Output is correct
20 Correct 7 ms 23112 KB Output is correct
21 Correct 7 ms 23132 KB Output is correct
22 Correct 7 ms 23384 KB Output is correct
23 Correct 316 ms 108540 KB Output is correct
24 Correct 6 ms 23132 KB Output is correct
25 Correct 8 ms 23644 KB Output is correct
26 Correct 8 ms 23132 KB Output is correct
27 Correct 7 ms 23132 KB Output is correct
28 Correct 120 ms 55556 KB Output is correct
29 Correct 190 ms 74096 KB Output is correct
30 Correct 238 ms 88364 KB Output is correct
31 Correct 327 ms 108476 KB Output is correct
32 Correct 7 ms 23128 KB Output is correct
33 Correct 7 ms 23128 KB Output is correct
34 Correct 7 ms 22876 KB Output is correct
35 Correct 8 ms 23132 KB Output is correct
36 Correct 7 ms 23132 KB Output is correct
37 Correct 6 ms 22956 KB Output is correct
38 Correct 6 ms 23132 KB Output is correct
39 Correct 7 ms 23132 KB Output is correct
40 Correct 7 ms 23120 KB Output is correct
41 Correct 7 ms 22972 KB Output is correct
42 Correct 6 ms 23132 KB Output is correct
43 Correct 7 ms 23136 KB Output is correct
44 Correct 8 ms 23132 KB Output is correct
45 Correct 164 ms 68188 KB Output is correct
46 Correct 249 ms 88776 KB Output is correct
47 Correct 257 ms 88720 KB Output is correct
48 Correct 6 ms 23132 KB Output is correct
49 Correct 6 ms 23132 KB Output is correct
50 Correct 7 ms 23132 KB Output is correct
51 Correct 7 ms 23128 KB Output is correct
52 Correct 7 ms 23132 KB Output is correct
53 Correct 7 ms 23132 KB Output is correct
54 Correct 7 ms 23120 KB Output is correct
55 Correct 334 ms 106120 KB Output is correct
56 Correct 7 ms 23132 KB Output is correct
57 Correct 74 ms 23796 KB Output is correct
58 Correct 15 ms 25688 KB Output is correct
59 Correct 9 ms 23640 KB Output is correct
60 Execution timed out 3530 ms 59720 KB Time limit exceeded
61 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 7 ms 23132 KB Output is correct
4 Correct 7 ms 23128 KB Output is correct
5 Correct 6 ms 23132 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 23132 KB Output is correct
8 Correct 7 ms 23132 KB Output is correct
9 Correct 146 ms 66548 KB Output is correct
10 Correct 22 ms 27188 KB Output is correct
11 Correct 72 ms 46192 KB Output is correct
12 Correct 24 ms 29476 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 9 ms 23132 KB Output is correct
16 Correct 160 ms 66520 KB Output is correct
17 Correct 7 ms 23384 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 7 ms 23132 KB Output is correct
20 Correct 368 ms 134024 KB Output is correct
21 Correct 328 ms 117184 KB Output is correct
22 Correct 419 ms 117196 KB Output is correct
23 Correct 308 ms 109604 KB Output is correct
24 Correct 60 ms 34380 KB Output is correct
25 Correct 58 ms 34384 KB Output is correct
26 Correct 59 ms 34440 KB Output is correct
27 Correct 365 ms 122528 KB Output is correct
28 Correct 385 ms 122392 KB Output is correct
29 Correct 387 ms 124136 KB Output is correct
30 Correct 381 ms 124056 KB Output is correct
31 Correct 7 ms 23128 KB Output is correct
32 Incorrect 3411 ms 26188 KB Solution announced impossible, but it is possible.
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 7 ms 23132 KB Output is correct
4 Correct 7 ms 23128 KB Output is correct
5 Correct 6 ms 23132 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 23132 KB Output is correct
8 Correct 7 ms 23132 KB Output is correct
9 Correct 146 ms 66548 KB Output is correct
10 Correct 22 ms 27188 KB Output is correct
11 Correct 72 ms 46192 KB Output is correct
12 Correct 24 ms 29476 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 9 ms 23132 KB Output is correct
16 Correct 160 ms 66520 KB Output is correct
17 Correct 369 ms 121280 KB Output is correct
18 Correct 394 ms 129460 KB Output is correct
19 Correct 341 ms 117216 KB Output is correct
20 Correct 364 ms 109028 KB Output is correct
21 Correct 357 ms 110528 KB Output is correct
22 Correct 7 ms 23128 KB Output is correct
23 Incorrect 3418 ms 30180 KB Solution announced impossible, but it is possible.
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 7 ms 23132 KB Output is correct
4 Correct 7 ms 23128 KB Output is correct
5 Correct 6 ms 23132 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 23132 KB Output is correct
8 Correct 7 ms 23132 KB Output is correct
9 Correct 146 ms 66548 KB Output is correct
10 Correct 22 ms 27188 KB Output is correct
11 Correct 72 ms 46192 KB Output is correct
12 Correct 24 ms 29476 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 9 ms 23132 KB Output is correct
16 Correct 160 ms 66520 KB Output is correct
17 Correct 6 ms 23128 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 7 ms 23128 KB Output is correct
20 Correct 7 ms 23112 KB Output is correct
21 Correct 7 ms 23132 KB Output is correct
22 Correct 7 ms 23384 KB Output is correct
23 Correct 316 ms 108540 KB Output is correct
24 Correct 6 ms 23132 KB Output is correct
25 Correct 8 ms 23644 KB Output is correct
26 Correct 8 ms 23132 KB Output is correct
27 Correct 7 ms 23132 KB Output is correct
28 Correct 120 ms 55556 KB Output is correct
29 Correct 190 ms 74096 KB Output is correct
30 Correct 238 ms 88364 KB Output is correct
31 Correct 327 ms 108476 KB Output is correct
32 Correct 7 ms 23128 KB Output is correct
33 Correct 7 ms 23128 KB Output is correct
34 Correct 7 ms 22876 KB Output is correct
35 Correct 8 ms 23132 KB Output is correct
36 Correct 7 ms 23132 KB Output is correct
37 Correct 6 ms 22956 KB Output is correct
38 Correct 6 ms 23132 KB Output is correct
39 Correct 7 ms 23132 KB Output is correct
40 Correct 7 ms 23120 KB Output is correct
41 Correct 7 ms 22972 KB Output is correct
42 Correct 6 ms 23132 KB Output is correct
43 Correct 7 ms 23136 KB Output is correct
44 Correct 8 ms 23132 KB Output is correct
45 Correct 164 ms 68188 KB Output is correct
46 Correct 249 ms 88776 KB Output is correct
47 Correct 257 ms 88720 KB Output is correct
48 Correct 6 ms 23132 KB Output is correct
49 Correct 6 ms 23132 KB Output is correct
50 Correct 7 ms 23132 KB Output is correct
51 Correct 7 ms 23128 KB Output is correct
52 Correct 7 ms 23132 KB Output is correct
53 Correct 7 ms 23132 KB Output is correct
54 Correct 7 ms 23120 KB Output is correct
55 Correct 334 ms 106120 KB Output is correct
56 Correct 7 ms 23132 KB Output is correct
57 Correct 74 ms 23796 KB Output is correct
58 Correct 15 ms 25688 KB Output is correct
59 Correct 9 ms 23640 KB Output is correct
60 Execution timed out 3530 ms 59720 KB Time limit exceeded
61 Halted 0 ms 0 KB -