Submission #932686

# Submission time Handle Problem Language Result Execution time Memory
932686 2024-02-24T03:12:12 Z Programmer123 Fountain Parks (IOI21_parks) C++17
55 / 100
3500 ms 131320 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 = false;
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;
        if (shuffle) {
            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;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23128 KB Output is correct
2 Correct 7 ms 23128 KB Output is correct
3 Correct 6 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 7 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 127 ms 66656 KB Output is correct
10 Correct 20 ms 27228 KB Output is correct
11 Correct 65 ms 46096 KB Output is correct
12 Correct 26 ms 29532 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 7 ms 23384 KB Output is correct
16 Correct 130 ms 66564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23128 KB Output is correct
2 Correct 7 ms 23128 KB Output is correct
3 Correct 6 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 7 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 127 ms 66656 KB Output is correct
10 Correct 20 ms 27228 KB Output is correct
11 Correct 65 ms 46096 KB Output is correct
12 Correct 26 ms 29532 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 7 ms 23384 KB Output is correct
16 Correct 130 ms 66564 KB Output is correct
17 Correct 7 ms 23132 KB Output is correct
18 Correct 7 ms 23116 KB Output is correct
19 Correct 7 ms 23132 KB Output is correct
20 Correct 7 ms 23132 KB Output is correct
21 Correct 8 ms 22960 KB Output is correct
22 Correct 6 ms 23128 KB Output is correct
23 Correct 296 ms 107004 KB Output is correct
24 Correct 7 ms 23128 KB Output is correct
25 Correct 9 ms 23644 KB Output is correct
26 Correct 7 ms 23132 KB Output is correct
27 Correct 8 ms 23128 KB Output is correct
28 Correct 102 ms 55344 KB Output is correct
29 Correct 171 ms 72816 KB Output is correct
30 Correct 213 ms 87568 KB Output is correct
31 Correct 286 ms 107000 KB Output is correct
32 Correct 7 ms 23132 KB Output is correct
33 Correct 7 ms 23132 KB Output is correct
34 Correct 7 ms 23132 KB Output is correct
35 Correct 7 ms 23132 KB Output is correct
36 Correct 8 ms 23132 KB Output is correct
37 Correct 8 ms 23132 KB Output is correct
38 Correct 7 ms 23132 KB Output is correct
39 Correct 7 ms 23128 KB Output is correct
40 Correct 7 ms 23132 KB Output is correct
41 Correct 7 ms 23132 KB Output is correct
42 Correct 7 ms 23132 KB Output is correct
43 Correct 8 ms 23132 KB Output is correct
44 Correct 7 ms 23132 KB Output is correct
45 Correct 179 ms 66908 KB Output is correct
46 Correct 327 ms 88072 KB Output is correct
47 Correct 231 ms 88248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23128 KB Output is correct
2 Correct 7 ms 23128 KB Output is correct
3 Correct 6 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 7 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 127 ms 66656 KB Output is correct
10 Correct 20 ms 27228 KB Output is correct
11 Correct 65 ms 46096 KB Output is correct
12 Correct 26 ms 29532 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 7 ms 23384 KB Output is correct
16 Correct 130 ms 66564 KB Output is correct
17 Correct 7 ms 23132 KB Output is correct
18 Correct 7 ms 23116 KB Output is correct
19 Correct 7 ms 23132 KB Output is correct
20 Correct 7 ms 23132 KB Output is correct
21 Correct 8 ms 22960 KB Output is correct
22 Correct 6 ms 23128 KB Output is correct
23 Correct 296 ms 107004 KB Output is correct
24 Correct 7 ms 23128 KB Output is correct
25 Correct 9 ms 23644 KB Output is correct
26 Correct 7 ms 23132 KB Output is correct
27 Correct 8 ms 23128 KB Output is correct
28 Correct 102 ms 55344 KB Output is correct
29 Correct 171 ms 72816 KB Output is correct
30 Correct 213 ms 87568 KB Output is correct
31 Correct 286 ms 107000 KB Output is correct
32 Correct 7 ms 23132 KB Output is correct
33 Correct 7 ms 23132 KB Output is correct
34 Correct 7 ms 23132 KB Output is correct
35 Correct 7 ms 23132 KB Output is correct
36 Correct 8 ms 23132 KB Output is correct
37 Correct 8 ms 23132 KB Output is correct
38 Correct 7 ms 23132 KB Output is correct
39 Correct 7 ms 23128 KB Output is correct
40 Correct 7 ms 23132 KB Output is correct
41 Correct 7 ms 23132 KB Output is correct
42 Correct 7 ms 23132 KB Output is correct
43 Correct 8 ms 23132 KB Output is correct
44 Correct 7 ms 23132 KB Output is correct
45 Correct 179 ms 66908 KB Output is correct
46 Correct 327 ms 88072 KB Output is correct
47 Correct 231 ms 88248 KB Output is correct
48 Correct 9 ms 23128 KB Output is correct
49 Correct 7 ms 23132 KB Output is correct
50 Correct 7 ms 22952 KB Output is correct
51 Correct 7 ms 23132 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 23128 KB Output is correct
55 Correct 295 ms 104632 KB Output is correct
56 Correct 7 ms 23128 KB Output is correct
57 Correct 2251 ms 23840 KB Output is correct
58 Correct 22 ms 25680 KB Output is correct
59 Correct 12 ms 23896 KB Output is correct
60 Correct 143 ms 63752 KB Output is correct
61 Correct 2079 ms 83488 KB Output is correct
62 Correct 229 ms 91304 KB Output is correct
63 Execution timed out 3550 ms 96640 KB Time limit exceeded
64 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23128 KB Output is correct
2 Correct 7 ms 23128 KB Output is correct
3 Correct 6 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 7 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 127 ms 66656 KB Output is correct
10 Correct 20 ms 27228 KB Output is correct
11 Correct 65 ms 46096 KB Output is correct
12 Correct 26 ms 29532 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 7 ms 23384 KB Output is correct
16 Correct 130 ms 66564 KB Output is correct
17 Correct 7 ms 23132 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 7 ms 23132 KB Output is correct
20 Correct 335 ms 131320 KB Output is correct
21 Correct 350 ms 115064 KB Output is correct
22 Correct 366 ms 114940 KB Output is correct
23 Correct 304 ms 108436 KB Output is correct
24 Correct 62 ms 34388 KB Output is correct
25 Correct 65 ms 34388 KB Output is correct
26 Correct 60 ms 34384 KB Output is correct
27 Correct 385 ms 121068 KB Output is correct
28 Correct 394 ms 120808 KB Output is correct
29 Correct 386 ms 122600 KB Output is correct
30 Correct 431 ms 122924 KB Output is correct
31 Correct 7 ms 23132 KB Output is correct
32 Correct 29 ms 29532 KB Output is correct
33 Correct 49 ms 38740 KB Output is correct
34 Correct 349 ms 131272 KB Output is correct
35 Correct 11 ms 23640 KB Output is correct
36 Correct 22 ms 26204 KB Output is correct
37 Correct 37 ms 29268 KB Output is correct
38 Correct 149 ms 57588 KB Output is correct
39 Correct 234 ms 70456 KB Output is correct
40 Correct 285 ms 83436 KB Output is correct
41 Correct 369 ms 96520 KB Output is correct
42 Correct 441 ms 108892 KB Output is correct
43 Correct 7 ms 23128 KB Output is correct
44 Correct 6 ms 23128 KB Output is correct
45 Correct 7 ms 23128 KB Output is correct
46 Correct 7 ms 23132 KB Output is correct
47 Correct 7 ms 23132 KB Output is correct
48 Correct 7 ms 23132 KB Output is correct
49 Correct 7 ms 23132 KB Output is correct
50 Correct 7 ms 23132 KB Output is correct
51 Correct 7 ms 23132 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 23128 KB Output is correct
55 Correct 7 ms 23132 KB Output is correct
56 Correct 155 ms 67096 KB Output is correct
57 Correct 262 ms 88020 KB Output is correct
58 Correct 243 ms 88280 KB Output is correct
59 Correct 7 ms 23128 KB Output is correct
60 Correct 7 ms 23128 KB Output is correct
61 Correct 7 ms 23132 KB Output is correct
62 Correct 259 ms 106964 KB Output is correct
63 Correct 254 ms 106852 KB Output is correct
64 Correct 279 ms 107144 KB Output is correct
65 Correct 9 ms 23388 KB Output is correct
66 Correct 9 ms 23660 KB Output is correct
67 Correct 154 ms 66324 KB Output is correct
68 Correct 242 ms 87492 KB Output is correct
69 Correct 335 ms 109492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23128 KB Output is correct
2 Correct 7 ms 23128 KB Output is correct
3 Correct 6 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 7 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 127 ms 66656 KB Output is correct
10 Correct 20 ms 27228 KB Output is correct
11 Correct 65 ms 46096 KB Output is correct
12 Correct 26 ms 29532 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 7 ms 23384 KB Output is correct
16 Correct 130 ms 66564 KB Output is correct
17 Correct 344 ms 118984 KB Output is correct
18 Correct 446 ms 127612 KB Output is correct
19 Correct 329 ms 114924 KB Output is correct
20 Correct 379 ms 107568 KB Output is correct
21 Correct 423 ms 108812 KB Output is correct
22 Correct 7 ms 23128 KB Output is correct
23 Correct 55 ms 37320 KB Output is correct
24 Correct 16 ms 24408 KB Output is correct
25 Correct 28 ms 27484 KB Output is correct
26 Correct 51 ms 30300 KB Output is correct
27 Correct 188 ms 67192 KB Output is correct
28 Correct 246 ms 78512 KB Output is correct
29 Correct 302 ms 88556 KB Output is correct
30 Correct 357 ms 99304 KB Output is correct
31 Correct 433 ms 109932 KB Output is correct
32 Correct 322 ms 110696 KB Output is correct
33 Correct 262 ms 106848 KB Output is correct
34 Correct 8 ms 23388 KB Output is correct
35 Correct 9 ms 23644 KB Output is correct
36 Correct 145 ms 65856 KB Output is correct
37 Correct 249 ms 87468 KB Output is correct
38 Correct 366 ms 108860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23128 KB Output is correct
2 Correct 7 ms 23128 KB Output is correct
3 Correct 6 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 7 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 127 ms 66656 KB Output is correct
10 Correct 20 ms 27228 KB Output is correct
11 Correct 65 ms 46096 KB Output is correct
12 Correct 26 ms 29532 KB Output is correct
13 Correct 17 ms 26220 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 7 ms 23384 KB Output is correct
16 Correct 130 ms 66564 KB Output is correct
17 Correct 7 ms 23132 KB Output is correct
18 Correct 7 ms 23116 KB Output is correct
19 Correct 7 ms 23132 KB Output is correct
20 Correct 7 ms 23132 KB Output is correct
21 Correct 8 ms 22960 KB Output is correct
22 Correct 6 ms 23128 KB Output is correct
23 Correct 296 ms 107004 KB Output is correct
24 Correct 7 ms 23128 KB Output is correct
25 Correct 9 ms 23644 KB Output is correct
26 Correct 7 ms 23132 KB Output is correct
27 Correct 8 ms 23128 KB Output is correct
28 Correct 102 ms 55344 KB Output is correct
29 Correct 171 ms 72816 KB Output is correct
30 Correct 213 ms 87568 KB Output is correct
31 Correct 286 ms 107000 KB Output is correct
32 Correct 7 ms 23132 KB Output is correct
33 Correct 7 ms 23132 KB Output is correct
34 Correct 7 ms 23132 KB Output is correct
35 Correct 7 ms 23132 KB Output is correct
36 Correct 8 ms 23132 KB Output is correct
37 Correct 8 ms 23132 KB Output is correct
38 Correct 7 ms 23132 KB Output is correct
39 Correct 7 ms 23128 KB Output is correct
40 Correct 7 ms 23132 KB Output is correct
41 Correct 7 ms 23132 KB Output is correct
42 Correct 7 ms 23132 KB Output is correct
43 Correct 8 ms 23132 KB Output is correct
44 Correct 7 ms 23132 KB Output is correct
45 Correct 179 ms 66908 KB Output is correct
46 Correct 327 ms 88072 KB Output is correct
47 Correct 231 ms 88248 KB Output is correct
48 Correct 9 ms 23128 KB Output is correct
49 Correct 7 ms 23132 KB Output is correct
50 Correct 7 ms 22952 KB Output is correct
51 Correct 7 ms 23132 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 23128 KB Output is correct
55 Correct 295 ms 104632 KB Output is correct
56 Correct 7 ms 23128 KB Output is correct
57 Correct 2251 ms 23840 KB Output is correct
58 Correct 22 ms 25680 KB Output is correct
59 Correct 12 ms 23896 KB Output is correct
60 Correct 143 ms 63752 KB Output is correct
61 Correct 2079 ms 83488 KB Output is correct
62 Correct 229 ms 91304 KB Output is correct
63 Execution timed out 3550 ms 96640 KB Time limit exceeded
64 Halted 0 ms 0 KB -