답안 #932672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932672 2024-02-24T02:44:50 Z Programmer123 분수 공원 (IOI21_parks) C++17
55 / 100
3500 ms 147820 KB
#include <bits/stdc++.h>
#include "parks.h"

std::unordered_map<int, int> map[200001];
int N;
std::unordered_map<int, std::vector<int>> rely[200002];
std::vector<std::pair<int, int>> vld[200000];
std::pair<int, int> assigned[200000];
std::unordered_set<int> taken[200002];
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].count(a.second)) return false;
    return true;
}

bool cantake(std::pair<int, int> a, int n) {
    for (auto x: rely[a.first][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].count(a.second)) continue;
        taken[a.first].insert(a.second);
        if (!cantake(a, n)) {
            taken[a.first].erase(a.second);
            continue;
        }
        if (backtrack(n + 1)) {
            assigned[n] = a;
            return true;
        }
        taken[a.first].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]][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].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);
            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][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].clear();
            rely[_x + 1].clear();
        }
        if (clock() > stop) break;
        Shuffle = shuffle;
        shuffle = true;
#ifdef LOCAL
        printf("Retrying...\n");
#endif
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 39512 KB Output is correct
2 Correct 10 ms 39512 KB Output is correct
3 Correct 11 ms 39516 KB Output is correct
4 Correct 10 ms 39572 KB Output is correct
5 Correct 10 ms 39516 KB Output is correct
6 Correct 10 ms 39516 KB Output is correct
7 Correct 12 ms 39512 KB Output is correct
8 Correct 10 ms 39512 KB Output is correct
9 Correct 133 ms 83120 KB Output is correct
10 Correct 23 ms 43868 KB Output is correct
11 Correct 69 ms 62652 KB Output is correct
12 Correct 27 ms 45916 KB Output is correct
13 Correct 21 ms 42600 KB Output is correct
14 Correct 10 ms 39512 KB Output is correct
15 Correct 11 ms 39516 KB Output is correct
16 Correct 130 ms 83124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 39512 KB Output is correct
2 Correct 10 ms 39512 KB Output is correct
3 Correct 11 ms 39516 KB Output is correct
4 Correct 10 ms 39572 KB Output is correct
5 Correct 10 ms 39516 KB Output is correct
6 Correct 10 ms 39516 KB Output is correct
7 Correct 12 ms 39512 KB Output is correct
8 Correct 10 ms 39512 KB Output is correct
9 Correct 133 ms 83120 KB Output is correct
10 Correct 23 ms 43868 KB Output is correct
11 Correct 69 ms 62652 KB Output is correct
12 Correct 27 ms 45916 KB Output is correct
13 Correct 21 ms 42600 KB Output is correct
14 Correct 10 ms 39512 KB Output is correct
15 Correct 11 ms 39516 KB Output is correct
16 Correct 130 ms 83124 KB Output is correct
17 Correct 10 ms 39516 KB Output is correct
18 Correct 10 ms 39524 KB Output is correct
19 Correct 10 ms 39516 KB Output is correct
20 Correct 10 ms 39364 KB Output is correct
21 Correct 10 ms 39516 KB Output is correct
22 Correct 10 ms 39516 KB Output is correct
23 Correct 278 ms 123364 KB Output is correct
24 Correct 10 ms 39512 KB Output is correct
25 Correct 12 ms 40028 KB Output is correct
26 Correct 11 ms 39768 KB Output is correct
27 Correct 11 ms 39772 KB Output is correct
28 Correct 107 ms 71836 KB Output is correct
29 Correct 161 ms 88688 KB Output is correct
30 Correct 223 ms 103864 KB Output is correct
31 Correct 285 ms 123360 KB Output is correct
32 Correct 10 ms 39516 KB Output is correct
33 Correct 10 ms 39516 KB Output is correct
34 Correct 10 ms 39516 KB Output is correct
35 Correct 10 ms 39516 KB Output is correct
36 Correct 10 ms 39516 KB Output is correct
37 Correct 11 ms 39516 KB Output is correct
38 Correct 10 ms 39360 KB Output is correct
39 Correct 11 ms 39516 KB Output is correct
40 Correct 10 ms 39516 KB Output is correct
41 Correct 10 ms 39516 KB Output is correct
42 Correct 10 ms 39516 KB Output is correct
43 Correct 11 ms 39516 KB Output is correct
44 Correct 11 ms 39624 KB Output is correct
45 Correct 157 ms 83568 KB Output is correct
46 Correct 222 ms 104884 KB Output is correct
47 Correct 258 ms 104888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 39512 KB Output is correct
2 Correct 10 ms 39512 KB Output is correct
3 Correct 11 ms 39516 KB Output is correct
4 Correct 10 ms 39572 KB Output is correct
5 Correct 10 ms 39516 KB Output is correct
6 Correct 10 ms 39516 KB Output is correct
7 Correct 12 ms 39512 KB Output is correct
8 Correct 10 ms 39512 KB Output is correct
9 Correct 133 ms 83120 KB Output is correct
10 Correct 23 ms 43868 KB Output is correct
11 Correct 69 ms 62652 KB Output is correct
12 Correct 27 ms 45916 KB Output is correct
13 Correct 21 ms 42600 KB Output is correct
14 Correct 10 ms 39512 KB Output is correct
15 Correct 11 ms 39516 KB Output is correct
16 Correct 130 ms 83124 KB Output is correct
17 Correct 10 ms 39516 KB Output is correct
18 Correct 10 ms 39524 KB Output is correct
19 Correct 10 ms 39516 KB Output is correct
20 Correct 10 ms 39364 KB Output is correct
21 Correct 10 ms 39516 KB Output is correct
22 Correct 10 ms 39516 KB Output is correct
23 Correct 278 ms 123364 KB Output is correct
24 Correct 10 ms 39512 KB Output is correct
25 Correct 12 ms 40028 KB Output is correct
26 Correct 11 ms 39768 KB Output is correct
27 Correct 11 ms 39772 KB Output is correct
28 Correct 107 ms 71836 KB Output is correct
29 Correct 161 ms 88688 KB Output is correct
30 Correct 223 ms 103864 KB Output is correct
31 Correct 285 ms 123360 KB Output is correct
32 Correct 10 ms 39516 KB Output is correct
33 Correct 10 ms 39516 KB Output is correct
34 Correct 10 ms 39516 KB Output is correct
35 Correct 10 ms 39516 KB Output is correct
36 Correct 10 ms 39516 KB Output is correct
37 Correct 11 ms 39516 KB Output is correct
38 Correct 10 ms 39360 KB Output is correct
39 Correct 11 ms 39516 KB Output is correct
40 Correct 10 ms 39516 KB Output is correct
41 Correct 10 ms 39516 KB Output is correct
42 Correct 10 ms 39516 KB Output is correct
43 Correct 11 ms 39516 KB Output is correct
44 Correct 11 ms 39624 KB Output is correct
45 Correct 157 ms 83568 KB Output is correct
46 Correct 222 ms 104884 KB Output is correct
47 Correct 258 ms 104888 KB Output is correct
48 Correct 10 ms 39516 KB Output is correct
49 Correct 10 ms 39524 KB Output is correct
50 Correct 10 ms 39372 KB Output is correct
51 Correct 10 ms 39768 KB Output is correct
52 Correct 10 ms 39516 KB Output is correct
53 Correct 10 ms 39564 KB Output is correct
54 Correct 10 ms 39516 KB Output is correct
55 Correct 289 ms 121000 KB Output is correct
56 Correct 10 ms 39516 KB Output is correct
57 Correct 2045 ms 40308 KB Output is correct
58 Correct 1553 ms 42316 KB Output is correct
59 Correct 13 ms 40028 KB Output is correct
60 Correct 144 ms 80192 KB Output is correct
61 Correct 2114 ms 99972 KB Output is correct
62 Correct 242 ms 107692 KB Output is correct
63 Execution timed out 3544 ms 113024 KB Time limit exceeded
64 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 39512 KB Output is correct
2 Correct 10 ms 39512 KB Output is correct
3 Correct 11 ms 39516 KB Output is correct
4 Correct 10 ms 39572 KB Output is correct
5 Correct 10 ms 39516 KB Output is correct
6 Correct 10 ms 39516 KB Output is correct
7 Correct 12 ms 39512 KB Output is correct
8 Correct 10 ms 39512 KB Output is correct
9 Correct 133 ms 83120 KB Output is correct
10 Correct 23 ms 43868 KB Output is correct
11 Correct 69 ms 62652 KB Output is correct
12 Correct 27 ms 45916 KB Output is correct
13 Correct 21 ms 42600 KB Output is correct
14 Correct 10 ms 39512 KB Output is correct
15 Correct 11 ms 39516 KB Output is correct
16 Correct 130 ms 83124 KB Output is correct
17 Correct 12 ms 39512 KB Output is correct
18 Correct 11 ms 39512 KB Output is correct
19 Correct 10 ms 39512 KB Output is correct
20 Correct 356 ms 147820 KB Output is correct
21 Correct 347 ms 131404 KB Output is correct
22 Correct 345 ms 131796 KB Output is correct
23 Correct 299 ms 124816 KB Output is correct
24 Correct 69 ms 50772 KB Output is correct
25 Correct 65 ms 50876 KB Output is correct
26 Correct 64 ms 50704 KB Output is correct
27 Correct 417 ms 137360 KB Output is correct
28 Correct 430 ms 137348 KB Output is correct
29 Correct 425 ms 139172 KB Output is correct
30 Correct 465 ms 139108 KB Output is correct
31 Correct 13 ms 39516 KB Output is correct
32 Correct 32 ms 45916 KB Output is correct
33 Correct 61 ms 55196 KB Output is correct
34 Correct 354 ms 147716 KB Output is correct
35 Correct 15 ms 40028 KB Output is correct
36 Correct 26 ms 42624 KB Output is correct
37 Correct 43 ms 45684 KB Output is correct
38 Correct 149 ms 73976 KB Output is correct
39 Correct 225 ms 86892 KB Output is correct
40 Correct 281 ms 100060 KB Output is correct
41 Correct 368 ms 112964 KB Output is correct
42 Correct 440 ms 125276 KB Output is correct
43 Correct 10 ms 39516 KB Output is correct
44 Correct 11 ms 39516 KB Output is correct
45 Correct 10 ms 39516 KB Output is correct
46 Correct 11 ms 39516 KB Output is correct
47 Correct 10 ms 39516 KB Output is correct
48 Correct 11 ms 39512 KB Output is correct
49 Correct 10 ms 39512 KB Output is correct
50 Correct 10 ms 39516 KB Output is correct
51 Correct 10 ms 39516 KB Output is correct
52 Correct 11 ms 39516 KB Output is correct
53 Correct 10 ms 39512 KB Output is correct
54 Correct 11 ms 39512 KB Output is correct
55 Correct 11 ms 39772 KB Output is correct
56 Correct 159 ms 83572 KB Output is correct
57 Correct 226 ms 104548 KB Output is correct
58 Correct 230 ms 104632 KB Output is correct
59 Correct 10 ms 39516 KB Output is correct
60 Correct 11 ms 39516 KB Output is correct
61 Correct 12 ms 39516 KB Output is correct
62 Correct 265 ms 123508 KB Output is correct
63 Correct 257 ms 123492 KB Output is correct
64 Correct 263 ms 123748 KB Output is correct
65 Correct 11 ms 39768 KB Output is correct
66 Correct 14 ms 40028 KB Output is correct
67 Correct 158 ms 82696 KB Output is correct
68 Correct 249 ms 104052 KB Output is correct
69 Correct 345 ms 125884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 39512 KB Output is correct
2 Correct 10 ms 39512 KB Output is correct
3 Correct 11 ms 39516 KB Output is correct
4 Correct 10 ms 39572 KB Output is correct
5 Correct 10 ms 39516 KB Output is correct
6 Correct 10 ms 39516 KB Output is correct
7 Correct 12 ms 39512 KB Output is correct
8 Correct 10 ms 39512 KB Output is correct
9 Correct 133 ms 83120 KB Output is correct
10 Correct 23 ms 43868 KB Output is correct
11 Correct 69 ms 62652 KB Output is correct
12 Correct 27 ms 45916 KB Output is correct
13 Correct 21 ms 42600 KB Output is correct
14 Correct 10 ms 39512 KB Output is correct
15 Correct 11 ms 39516 KB Output is correct
16 Correct 130 ms 83124 KB Output is correct
17 Correct 334 ms 135320 KB Output is correct
18 Correct 398 ms 143760 KB Output is correct
19 Correct 349 ms 131400 KB Output is correct
20 Correct 388 ms 124600 KB Output is correct
21 Correct 390 ms 125120 KB Output is correct
22 Correct 10 ms 39516 KB Output is correct
23 Correct 59 ms 53852 KB Output is correct
24 Correct 17 ms 40792 KB Output is correct
25 Correct 31 ms 43856 KB Output is correct
26 Correct 46 ms 46680 KB Output is correct
27 Correct 201 ms 83520 KB Output is correct
28 Correct 252 ms 94660 KB Output is correct
29 Correct 354 ms 105192 KB Output is correct
30 Correct 355 ms 115832 KB Output is correct
31 Correct 446 ms 126440 KB Output is correct
32 Correct 325 ms 126828 KB Output is correct
33 Correct 262 ms 123492 KB Output is correct
34 Correct 12 ms 39772 KB Output is correct
35 Correct 13 ms 40028 KB Output is correct
36 Correct 151 ms 82188 KB Output is correct
37 Correct 279 ms 103896 KB Output is correct
38 Correct 320 ms 125208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 39512 KB Output is correct
2 Correct 10 ms 39512 KB Output is correct
3 Correct 11 ms 39516 KB Output is correct
4 Correct 10 ms 39572 KB Output is correct
5 Correct 10 ms 39516 KB Output is correct
6 Correct 10 ms 39516 KB Output is correct
7 Correct 12 ms 39512 KB Output is correct
8 Correct 10 ms 39512 KB Output is correct
9 Correct 133 ms 83120 KB Output is correct
10 Correct 23 ms 43868 KB Output is correct
11 Correct 69 ms 62652 KB Output is correct
12 Correct 27 ms 45916 KB Output is correct
13 Correct 21 ms 42600 KB Output is correct
14 Correct 10 ms 39512 KB Output is correct
15 Correct 11 ms 39516 KB Output is correct
16 Correct 130 ms 83124 KB Output is correct
17 Correct 10 ms 39516 KB Output is correct
18 Correct 10 ms 39524 KB Output is correct
19 Correct 10 ms 39516 KB Output is correct
20 Correct 10 ms 39364 KB Output is correct
21 Correct 10 ms 39516 KB Output is correct
22 Correct 10 ms 39516 KB Output is correct
23 Correct 278 ms 123364 KB Output is correct
24 Correct 10 ms 39512 KB Output is correct
25 Correct 12 ms 40028 KB Output is correct
26 Correct 11 ms 39768 KB Output is correct
27 Correct 11 ms 39772 KB Output is correct
28 Correct 107 ms 71836 KB Output is correct
29 Correct 161 ms 88688 KB Output is correct
30 Correct 223 ms 103864 KB Output is correct
31 Correct 285 ms 123360 KB Output is correct
32 Correct 10 ms 39516 KB Output is correct
33 Correct 10 ms 39516 KB Output is correct
34 Correct 10 ms 39516 KB Output is correct
35 Correct 10 ms 39516 KB Output is correct
36 Correct 10 ms 39516 KB Output is correct
37 Correct 11 ms 39516 KB Output is correct
38 Correct 10 ms 39360 KB Output is correct
39 Correct 11 ms 39516 KB Output is correct
40 Correct 10 ms 39516 KB Output is correct
41 Correct 10 ms 39516 KB Output is correct
42 Correct 10 ms 39516 KB Output is correct
43 Correct 11 ms 39516 KB Output is correct
44 Correct 11 ms 39624 KB Output is correct
45 Correct 157 ms 83568 KB Output is correct
46 Correct 222 ms 104884 KB Output is correct
47 Correct 258 ms 104888 KB Output is correct
48 Correct 10 ms 39516 KB Output is correct
49 Correct 10 ms 39524 KB Output is correct
50 Correct 10 ms 39372 KB Output is correct
51 Correct 10 ms 39768 KB Output is correct
52 Correct 10 ms 39516 KB Output is correct
53 Correct 10 ms 39564 KB Output is correct
54 Correct 10 ms 39516 KB Output is correct
55 Correct 289 ms 121000 KB Output is correct
56 Correct 10 ms 39516 KB Output is correct
57 Correct 2045 ms 40308 KB Output is correct
58 Correct 1553 ms 42316 KB Output is correct
59 Correct 13 ms 40028 KB Output is correct
60 Correct 144 ms 80192 KB Output is correct
61 Correct 2114 ms 99972 KB Output is correct
62 Correct 242 ms 107692 KB Output is correct
63 Execution timed out 3544 ms 113024 KB Time limit exceeded
64 Halted 0 ms 0 KB -