Submission #932646

# Submission time Handle Problem Language Result Execution time Memory
932646 2024-02-24T01:49:23 Z Programmer123 Fountain Parks (IOI21_parks) C++17
55 / 100
2814 ms 87756 KB
#include <bits/stdc++.h>
#include "parks.h"

std::unordered_map<int, int> map[200001];
int N;
std::vector<std::pair<int, int>> vld[200000];
std::pair<int, int> assigned[200000];
std::set<std::pair<int, int>> taken;
time_t stop;

bool backtrack(int n) {
    if (n == N - 1) return true;
    if (clock() > stop) return false;
    for (auto a: vld[n]) {
        if (taken.count(a)) continue;
        taken.insert(a);
        if (backtrack(n + 1)) {
            assigned[n] = a;
            return true;
        }
        taken.erase(a);
    }
    return false;
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
    stop = clock() + 3 * 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]);
    }
    std::vector<int> u, v, a, b;
    for (int i = 0; i < N; ++i) {
        map[x[i]][y[i]] = i;
    }
    bool seen[N];
    for (int i = 0; i < N; ++i) {
        seen[i] = false;
    }
    std::queue<int> bfs;
    bfs.push(0);
    seen[0] = true;
    auto add = [&](int x, int y, std::vector<int> &data) {
        if (x < 0 || x > 200000) return;
        if (map[x].count(y)) data.push_back(map[x][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);
        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}};
        }
    };
    for (int i = 0; i < N - 1; ++i) {
        vld[i] = valid(u[i], v[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;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 4 ms 17500 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 6 ms 17500 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 129 ms 49880 KB Output is correct
10 Correct 15 ms 20572 KB Output is correct
11 Correct 64 ms 34744 KB Output is correct
12 Correct 20 ms 22360 KB Output is correct
13 Correct 15 ms 20844 KB Output is correct
14 Correct 4 ms 17496 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 130 ms 49712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 4 ms 17500 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 6 ms 17500 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 129 ms 49880 KB Output is correct
10 Correct 15 ms 20572 KB Output is correct
11 Correct 64 ms 34744 KB Output is correct
12 Correct 20 ms 22360 KB Output is correct
13 Correct 15 ms 20844 KB Output is correct
14 Correct 4 ms 17496 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 130 ms 49712 KB Output is correct
17 Correct 4 ms 17496 KB Output is correct
18 Correct 4 ms 17500 KB Output is correct
19 Correct 4 ms 17500 KB Output is correct
20 Correct 4 ms 17500 KB Output is correct
21 Correct 4 ms 17500 KB Output is correct
22 Correct 4 ms 17500 KB Output is correct
23 Correct 278 ms 77972 KB Output is correct
24 Correct 5 ms 17500 KB Output is correct
25 Correct 5 ms 17748 KB Output is correct
26 Correct 5 ms 17500 KB Output is correct
27 Correct 5 ms 17568 KB Output is correct
28 Correct 105 ms 41576 KB Output is correct
29 Correct 160 ms 53720 KB Output is correct
30 Correct 211 ms 65388 KB Output is correct
31 Correct 278 ms 78352 KB Output is correct
32 Correct 4 ms 17496 KB Output is correct
33 Correct 4 ms 17500 KB Output is correct
34 Correct 4 ms 17500 KB Output is correct
35 Correct 4 ms 17500 KB Output is correct
36 Correct 5 ms 17500 KB Output is correct
37 Correct 4 ms 17500 KB Output is correct
38 Correct 4 ms 17500 KB Output is correct
39 Correct 4 ms 17496 KB Output is correct
40 Correct 5 ms 17500 KB Output is correct
41 Correct 4 ms 17500 KB Output is correct
42 Correct 4 ms 17500 KB Output is correct
43 Correct 5 ms 17500 KB Output is correct
44 Correct 5 ms 17500 KB Output is correct
45 Correct 128 ms 48124 KB Output is correct
46 Correct 189 ms 60868 KB Output is correct
47 Correct 190 ms 60828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 4 ms 17500 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 6 ms 17500 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 129 ms 49880 KB Output is correct
10 Correct 15 ms 20572 KB Output is correct
11 Correct 64 ms 34744 KB Output is correct
12 Correct 20 ms 22360 KB Output is correct
13 Correct 15 ms 20844 KB Output is correct
14 Correct 4 ms 17496 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 130 ms 49712 KB Output is correct
17 Correct 4 ms 17496 KB Output is correct
18 Correct 4 ms 17500 KB Output is correct
19 Correct 4 ms 17500 KB Output is correct
20 Correct 4 ms 17500 KB Output is correct
21 Correct 4 ms 17500 KB Output is correct
22 Correct 4 ms 17500 KB Output is correct
23 Correct 278 ms 77972 KB Output is correct
24 Correct 5 ms 17500 KB Output is correct
25 Correct 5 ms 17748 KB Output is correct
26 Correct 5 ms 17500 KB Output is correct
27 Correct 5 ms 17568 KB Output is correct
28 Correct 105 ms 41576 KB Output is correct
29 Correct 160 ms 53720 KB Output is correct
30 Correct 211 ms 65388 KB Output is correct
31 Correct 278 ms 78352 KB Output is correct
32 Correct 4 ms 17496 KB Output is correct
33 Correct 4 ms 17500 KB Output is correct
34 Correct 4 ms 17500 KB Output is correct
35 Correct 4 ms 17500 KB Output is correct
36 Correct 5 ms 17500 KB Output is correct
37 Correct 4 ms 17500 KB Output is correct
38 Correct 4 ms 17500 KB Output is correct
39 Correct 4 ms 17496 KB Output is correct
40 Correct 5 ms 17500 KB Output is correct
41 Correct 4 ms 17500 KB Output is correct
42 Correct 4 ms 17500 KB Output is correct
43 Correct 5 ms 17500 KB Output is correct
44 Correct 5 ms 17500 KB Output is correct
45 Correct 128 ms 48124 KB Output is correct
46 Correct 189 ms 60868 KB Output is correct
47 Correct 190 ms 60828 KB Output is correct
48 Correct 4 ms 17500 KB Output is correct
49 Correct 4 ms 17504 KB Output is correct
50 Correct 4 ms 17516 KB Output is correct
51 Correct 4 ms 17500 KB Output is correct
52 Correct 5 ms 17504 KB Output is correct
53 Correct 4 ms 17500 KB Output is correct
54 Correct 4 ms 17508 KB Output is correct
55 Correct 314 ms 77832 KB Output is correct
56 Correct 4 ms 17496 KB Output is correct
57 Correct 2814 ms 18052 KB Output is correct
58 Incorrect 6 ms 18360 KB Solution announced impossible, but it is possible.
59 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 4 ms 17500 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 6 ms 17500 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 129 ms 49880 KB Output is correct
10 Correct 15 ms 20572 KB Output is correct
11 Correct 64 ms 34744 KB Output is correct
12 Correct 20 ms 22360 KB Output is correct
13 Correct 15 ms 20844 KB Output is correct
14 Correct 4 ms 17496 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 130 ms 49712 KB Output is correct
17 Correct 4 ms 17500 KB Output is correct
18 Correct 4 ms 17500 KB Output is correct
19 Correct 4 ms 17500 KB Output is correct
20 Correct 330 ms 86724 KB Output is correct
21 Correct 312 ms 81952 KB Output is correct
22 Correct 314 ms 82152 KB Output is correct
23 Correct 273 ms 73572 KB Output is correct
24 Correct 59 ms 30288 KB Output is correct
25 Correct 59 ms 30192 KB Output is correct
26 Correct 59 ms 30448 KB Output is correct
27 Correct 330 ms 77600 KB Output is correct
28 Correct 324 ms 77708 KB Output is correct
29 Correct 341 ms 78036 KB Output is correct
30 Correct 340 ms 78060 KB Output is correct
31 Correct 5 ms 17752 KB Output is correct
32 Correct 23 ms 22284 KB Output is correct
33 Correct 50 ms 34392 KB Output is correct
34 Correct 328 ms 87756 KB Output is correct
35 Correct 7 ms 18268 KB Output is correct
36 Correct 18 ms 21084 KB Output is correct
37 Correct 32 ms 24924 KB Output is correct
38 Correct 132 ms 42232 KB Output is correct
39 Correct 196 ms 51652 KB Output is correct
40 Correct 242 ms 60900 KB Output is correct
41 Correct 307 ms 69968 KB Output is correct
42 Correct 357 ms 79144 KB Output is correct
43 Correct 4 ms 17500 KB Output is correct
44 Correct 4 ms 17500 KB Output is correct
45 Correct 4 ms 17496 KB Output is correct
46 Correct 4 ms 17500 KB Output is correct
47 Correct 4 ms 17500 KB Output is correct
48 Correct 4 ms 17500 KB Output is correct
49 Correct 4 ms 17500 KB Output is correct
50 Correct 4 ms 17500 KB Output is correct
51 Correct 4 ms 17500 KB Output is correct
52 Correct 5 ms 17500 KB Output is correct
53 Correct 4 ms 17500 KB Output is correct
54 Correct 5 ms 17500 KB Output is correct
55 Correct 5 ms 17500 KB Output is correct
56 Correct 130 ms 48248 KB Output is correct
57 Correct 185 ms 61340 KB Output is correct
58 Correct 188 ms 61504 KB Output is correct
59 Correct 4 ms 17500 KB Output is correct
60 Correct 4 ms 17496 KB Output is correct
61 Correct 4 ms 17496 KB Output is correct
62 Correct 261 ms 78528 KB Output is correct
63 Correct 258 ms 78740 KB Output is correct
64 Correct 271 ms 78452 KB Output is correct
65 Correct 5 ms 17756 KB Output is correct
66 Correct 6 ms 18008 KB Output is correct
67 Correct 138 ms 47712 KB Output is correct
68 Correct 207 ms 63404 KB Output is correct
69 Correct 289 ms 77832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 4 ms 17500 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 6 ms 17500 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 129 ms 49880 KB Output is correct
10 Correct 15 ms 20572 KB Output is correct
11 Correct 64 ms 34744 KB Output is correct
12 Correct 20 ms 22360 KB Output is correct
13 Correct 15 ms 20844 KB Output is correct
14 Correct 4 ms 17496 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 130 ms 49712 KB Output is correct
17 Correct 313 ms 82336 KB Output is correct
18 Correct 315 ms 82384 KB Output is correct
19 Correct 318 ms 82056 KB Output is correct
20 Correct 314 ms 76336 KB Output is correct
21 Correct 283 ms 71164 KB Output is correct
22 Correct 4 ms 17496 KB Output is correct
23 Correct 48 ms 27484 KB Output is correct
24 Correct 11 ms 19032 KB Output is correct
25 Correct 28 ms 22608 KB Output is correct
26 Correct 38 ms 26196 KB Output is correct
27 Correct 163 ms 48508 KB Output is correct
28 Correct 206 ms 56668 KB Output is correct
29 Correct 258 ms 63936 KB Output is correct
30 Correct 311 ms 71404 KB Output is correct
31 Correct 332 ms 79076 KB Output is correct
32 Correct 278 ms 77564 KB Output is correct
33 Correct 260 ms 78484 KB Output is correct
34 Correct 7 ms 17852 KB Output is correct
35 Correct 7 ms 18012 KB Output is correct
36 Correct 140 ms 47880 KB Output is correct
37 Correct 203 ms 63404 KB Output is correct
38 Correct 272 ms 77996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 4 ms 17500 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 6 ms 17500 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 129 ms 49880 KB Output is correct
10 Correct 15 ms 20572 KB Output is correct
11 Correct 64 ms 34744 KB Output is correct
12 Correct 20 ms 22360 KB Output is correct
13 Correct 15 ms 20844 KB Output is correct
14 Correct 4 ms 17496 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 130 ms 49712 KB Output is correct
17 Correct 4 ms 17496 KB Output is correct
18 Correct 4 ms 17500 KB Output is correct
19 Correct 4 ms 17500 KB Output is correct
20 Correct 4 ms 17500 KB Output is correct
21 Correct 4 ms 17500 KB Output is correct
22 Correct 4 ms 17500 KB Output is correct
23 Correct 278 ms 77972 KB Output is correct
24 Correct 5 ms 17500 KB Output is correct
25 Correct 5 ms 17748 KB Output is correct
26 Correct 5 ms 17500 KB Output is correct
27 Correct 5 ms 17568 KB Output is correct
28 Correct 105 ms 41576 KB Output is correct
29 Correct 160 ms 53720 KB Output is correct
30 Correct 211 ms 65388 KB Output is correct
31 Correct 278 ms 78352 KB Output is correct
32 Correct 4 ms 17496 KB Output is correct
33 Correct 4 ms 17500 KB Output is correct
34 Correct 4 ms 17500 KB Output is correct
35 Correct 4 ms 17500 KB Output is correct
36 Correct 5 ms 17500 KB Output is correct
37 Correct 4 ms 17500 KB Output is correct
38 Correct 4 ms 17500 KB Output is correct
39 Correct 4 ms 17496 KB Output is correct
40 Correct 5 ms 17500 KB Output is correct
41 Correct 4 ms 17500 KB Output is correct
42 Correct 4 ms 17500 KB Output is correct
43 Correct 5 ms 17500 KB Output is correct
44 Correct 5 ms 17500 KB Output is correct
45 Correct 128 ms 48124 KB Output is correct
46 Correct 189 ms 60868 KB Output is correct
47 Correct 190 ms 60828 KB Output is correct
48 Correct 4 ms 17500 KB Output is correct
49 Correct 4 ms 17504 KB Output is correct
50 Correct 4 ms 17516 KB Output is correct
51 Correct 4 ms 17500 KB Output is correct
52 Correct 5 ms 17504 KB Output is correct
53 Correct 4 ms 17500 KB Output is correct
54 Correct 4 ms 17508 KB Output is correct
55 Correct 314 ms 77832 KB Output is correct
56 Correct 4 ms 17496 KB Output is correct
57 Correct 2814 ms 18052 KB Output is correct
58 Incorrect 6 ms 18360 KB Solution announced impossible, but it is possible.
59 Halted 0 ms 0 KB -