Submission #932678

# Submission time Handle Problem Language Result Execution time Memory
932678 2024-02-24T02:53:11 Z Programmer123 Fountain Parks (IOI21_parks) C++17
55 / 100
3500 ms 131276 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;
}

bool backtrack(int n) {
    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) 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)) {
            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;
        }
        for (auto _x: x) {
            rely[(_x - 1) / 2].clear();
            rely[(_x + 1) / 2].clear();
        }
        Shuffle = shuffle;
        shuffle = true;

#ifdef LOCAL
        printf("Retrying...\n");
#endif
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 8 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 8 ms 23128 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 22960 KB Output is correct
8 Correct 7 ms 23128 KB Output is correct
9 Correct 126 ms 66752 KB Output is correct
10 Correct 18 ms 27224 KB Output is correct
11 Correct 66 ms 46008 KB Output is correct
12 Correct 24 ms 29532 KB Output is correct
13 Correct 17 ms 26216 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 8 ms 23128 KB Output is correct
16 Correct 128 ms 66604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 8 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 8 ms 23128 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 22960 KB Output is correct
8 Correct 7 ms 23128 KB Output is correct
9 Correct 126 ms 66752 KB Output is correct
10 Correct 18 ms 27224 KB Output is correct
11 Correct 66 ms 46008 KB Output is correct
12 Correct 24 ms 29532 KB Output is correct
13 Correct 17 ms 26216 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 8 ms 23128 KB Output is correct
16 Correct 128 ms 66604 KB Output is correct
17 Correct 9 ms 23384 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 8 ms 22968 KB Output is correct
20 Correct 8 ms 23132 KB Output is correct
21 Correct 8 ms 23132 KB Output is correct
22 Correct 7 ms 23132 KB Output is correct
23 Correct 275 ms 107008 KB Output is correct
24 Correct 7 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 8 ms 23132 KB Output is correct
28 Correct 105 ms 55368 KB Output is correct
29 Correct 158 ms 72248 KB Output is correct
30 Correct 211 ms 87524 KB Output is correct
31 Correct 272 ms 107168 KB Output is correct
32 Correct 7 ms 23128 KB Output is correct
33 Correct 8 ms 23132 KB Output is correct
34 Correct 7 ms 23132 KB Output is correct
35 Correct 7 ms 23128 KB Output is correct
36 Correct 7 ms 23132 KB Output is correct
37 Correct 8 ms 22872 KB Output is correct
38 Correct 8 ms 23128 KB Output is correct
39 Correct 7 ms 23132 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 7 ms 23132 KB Output is correct
44 Correct 11 ms 23128 KB Output is correct
45 Correct 160 ms 67028 KB Output is correct
46 Correct 234 ms 88280 KB Output is correct
47 Correct 229 ms 88252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 8 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 8 ms 23128 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 22960 KB Output is correct
8 Correct 7 ms 23128 KB Output is correct
9 Correct 126 ms 66752 KB Output is correct
10 Correct 18 ms 27224 KB Output is correct
11 Correct 66 ms 46008 KB Output is correct
12 Correct 24 ms 29532 KB Output is correct
13 Correct 17 ms 26216 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 8 ms 23128 KB Output is correct
16 Correct 128 ms 66604 KB Output is correct
17 Correct 9 ms 23384 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 8 ms 22968 KB Output is correct
20 Correct 8 ms 23132 KB Output is correct
21 Correct 8 ms 23132 KB Output is correct
22 Correct 7 ms 23132 KB Output is correct
23 Correct 275 ms 107008 KB Output is correct
24 Correct 7 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 8 ms 23132 KB Output is correct
28 Correct 105 ms 55368 KB Output is correct
29 Correct 158 ms 72248 KB Output is correct
30 Correct 211 ms 87524 KB Output is correct
31 Correct 272 ms 107168 KB Output is correct
32 Correct 7 ms 23128 KB Output is correct
33 Correct 8 ms 23132 KB Output is correct
34 Correct 7 ms 23132 KB Output is correct
35 Correct 7 ms 23128 KB Output is correct
36 Correct 7 ms 23132 KB Output is correct
37 Correct 8 ms 22872 KB Output is correct
38 Correct 8 ms 23128 KB Output is correct
39 Correct 7 ms 23132 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 7 ms 23132 KB Output is correct
44 Correct 11 ms 23128 KB Output is correct
45 Correct 160 ms 67028 KB Output is correct
46 Correct 234 ms 88280 KB Output is correct
47 Correct 229 ms 88252 KB Output is correct
48 Correct 7 ms 23128 KB Output is correct
49 Correct 7 ms 23132 KB Output is correct
50 Correct 7 ms 23132 KB Output is correct
51 Correct 9 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 23132 KB Output is correct
55 Correct 286 ms 104880 KB Output is correct
56 Correct 7 ms 23132 KB Output is correct
57 Correct 2058 ms 23852 KB Output is correct
58 Correct 1633 ms 25872 KB Output is correct
59 Correct 10 ms 23640 KB Output is correct
60 Correct 132 ms 63752 KB Output is correct
61 Correct 2061 ms 83520 KB Output is correct
62 Correct 227 ms 91304 KB Output is correct
63 Execution timed out 3543 ms 96640 KB Time limit exceeded
64 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 8 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 8 ms 23128 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 22960 KB Output is correct
8 Correct 7 ms 23128 KB Output is correct
9 Correct 126 ms 66752 KB Output is correct
10 Correct 18 ms 27224 KB Output is correct
11 Correct 66 ms 46008 KB Output is correct
12 Correct 24 ms 29532 KB Output is correct
13 Correct 17 ms 26216 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 8 ms 23128 KB Output is correct
16 Correct 128 ms 66604 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 358 ms 131276 KB Output is correct
21 Correct 335 ms 114892 KB Output is correct
22 Correct 328 ms 114920 KB Output is correct
23 Correct 323 ms 108532 KB Output is correct
24 Correct 62 ms 34388 KB Output is correct
25 Correct 68 ms 34428 KB Output is correct
26 Correct 72 ms 34280 KB Output is correct
27 Correct 394 ms 121052 KB Output is correct
28 Correct 394 ms 121064 KB Output is correct
29 Correct 389 ms 122768 KB Output is correct
30 Correct 401 ms 122856 KB Output is correct
31 Correct 7 ms 23132 KB Output is correct
32 Correct 29 ms 29452 KB Output is correct
33 Correct 53 ms 38848 KB Output is correct
34 Correct 366 ms 131260 KB Output is correct
35 Correct 10 ms 23640 KB Output is correct
36 Correct 22 ms 26204 KB Output is correct
37 Correct 38 ms 29268 KB Output is correct
38 Correct 142 ms 57492 KB Output is correct
39 Correct 218 ms 70488 KB Output is correct
40 Correct 273 ms 83760 KB Output is correct
41 Correct 337 ms 96644 KB Output is correct
42 Correct 410 ms 108780 KB Output is correct
43 Correct 7 ms 23132 KB Output is correct
44 Correct 7 ms 23132 KB Output is correct
45 Correct 7 ms 23132 KB Output is correct
46 Correct 7 ms 23132 KB Output is correct
47 Correct 8 ms 23132 KB Output is correct
48 Correct 7 ms 23128 KB Output is correct
49 Correct 7 ms 23132 KB Output is correct
50 Correct 9 ms 22948 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 8 ms 23128 KB Output is correct
55 Correct 8 ms 23132 KB Output is correct
56 Correct 148 ms 67088 KB Output is correct
57 Correct 236 ms 88252 KB Output is correct
58 Correct 221 ms 88248 KB Output is correct
59 Correct 7 ms 23128 KB Output is correct
60 Correct 7 ms 23132 KB Output is correct
61 Correct 8 ms 22964 KB Output is correct
62 Correct 274 ms 106888 KB Output is correct
63 Correct 252 ms 106848 KB Output is correct
64 Correct 258 ms 107108 KB Output is correct
65 Correct 9 ms 23388 KB Output is correct
66 Correct 9 ms 23644 KB Output is correct
67 Correct 150 ms 66268 KB Output is correct
68 Correct 238 ms 87468 KB Output is correct
69 Correct 325 ms 109416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 8 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 8 ms 23128 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 22960 KB Output is correct
8 Correct 7 ms 23128 KB Output is correct
9 Correct 126 ms 66752 KB Output is correct
10 Correct 18 ms 27224 KB Output is correct
11 Correct 66 ms 46008 KB Output is correct
12 Correct 24 ms 29532 KB Output is correct
13 Correct 17 ms 26216 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 8 ms 23128 KB Output is correct
16 Correct 128 ms 66604 KB Output is correct
17 Correct 328 ms 119016 KB Output is correct
18 Correct 381 ms 127324 KB Output is correct
19 Correct 339 ms 114928 KB Output is correct
20 Correct 410 ms 107568 KB Output is correct
21 Correct 356 ms 108568 KB Output is correct
22 Correct 7 ms 23128 KB Output is correct
23 Correct 56 ms 37268 KB Output is correct
24 Correct 14 ms 24412 KB Output is correct
25 Correct 28 ms 27480 KB Output is correct
26 Correct 43 ms 30376 KB Output is correct
27 Correct 188 ms 67176 KB Output is correct
28 Correct 241 ms 78344 KB Output is correct
29 Correct 318 ms 88808 KB Output is correct
30 Correct 337 ms 99304 KB Output is correct
31 Correct 392 ms 110056 KB Output is correct
32 Correct 307 ms 110324 KB Output is correct
33 Correct 269 ms 106976 KB Output is correct
34 Correct 8 ms 23384 KB Output is correct
35 Correct 9 ms 23644 KB Output is correct
36 Correct 146 ms 65760 KB Output is correct
37 Correct 246 ms 87512 KB Output is correct
38 Correct 330 ms 108884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23132 KB Output is correct
2 Correct 7 ms 23132 KB Output is correct
3 Correct 8 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 8 ms 23128 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 7 ms 22960 KB Output is correct
8 Correct 7 ms 23128 KB Output is correct
9 Correct 126 ms 66752 KB Output is correct
10 Correct 18 ms 27224 KB Output is correct
11 Correct 66 ms 46008 KB Output is correct
12 Correct 24 ms 29532 KB Output is correct
13 Correct 17 ms 26216 KB Output is correct
14 Correct 7 ms 23132 KB Output is correct
15 Correct 8 ms 23128 KB Output is correct
16 Correct 128 ms 66604 KB Output is correct
17 Correct 9 ms 23384 KB Output is correct
18 Correct 7 ms 23132 KB Output is correct
19 Correct 8 ms 22968 KB Output is correct
20 Correct 8 ms 23132 KB Output is correct
21 Correct 8 ms 23132 KB Output is correct
22 Correct 7 ms 23132 KB Output is correct
23 Correct 275 ms 107008 KB Output is correct
24 Correct 7 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 8 ms 23132 KB Output is correct
28 Correct 105 ms 55368 KB Output is correct
29 Correct 158 ms 72248 KB Output is correct
30 Correct 211 ms 87524 KB Output is correct
31 Correct 272 ms 107168 KB Output is correct
32 Correct 7 ms 23128 KB Output is correct
33 Correct 8 ms 23132 KB Output is correct
34 Correct 7 ms 23132 KB Output is correct
35 Correct 7 ms 23128 KB Output is correct
36 Correct 7 ms 23132 KB Output is correct
37 Correct 8 ms 22872 KB Output is correct
38 Correct 8 ms 23128 KB Output is correct
39 Correct 7 ms 23132 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 7 ms 23132 KB Output is correct
44 Correct 11 ms 23128 KB Output is correct
45 Correct 160 ms 67028 KB Output is correct
46 Correct 234 ms 88280 KB Output is correct
47 Correct 229 ms 88252 KB Output is correct
48 Correct 7 ms 23128 KB Output is correct
49 Correct 7 ms 23132 KB Output is correct
50 Correct 7 ms 23132 KB Output is correct
51 Correct 9 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 23132 KB Output is correct
55 Correct 286 ms 104880 KB Output is correct
56 Correct 7 ms 23132 KB Output is correct
57 Correct 2058 ms 23852 KB Output is correct
58 Correct 1633 ms 25872 KB Output is correct
59 Correct 10 ms 23640 KB Output is correct
60 Correct 132 ms 63752 KB Output is correct
61 Correct 2061 ms 83520 KB Output is correct
62 Correct 227 ms 91304 KB Output is correct
63 Execution timed out 3543 ms 96640 KB Time limit exceeded
64 Halted 0 ms 0 KB -