Submission #1068302

# Submission time Handle Problem Language Result Execution time Memory
1068302 2024-08-21T09:10:14 Z mc061 Mechanical Doll (IOI18_doll) C++17
65.6728 / 100
332 ms 43140 KB
// #pragma GCC optimize("O3")

#include <bits/stdc++.h>
using namespace std;
void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y);

map<int, pair<int, int>> sw;
int64_t h(int a, int b) {
    return (1ll*a*1e9)+b;
}
vector<int> get_ord(vector<vector<pair<int, bool>>>& tree, int root) {
    vector<bool> state(root+1, false);
    vector<bool> had(root+1, false);
    bool done = false;
    vector<int> ret;
    auto dfs = [&] (auto&& self, int v, bool fake) -> void {
        if (!tree[v].size()) {
            if (fake) return;
            if (had[v]) {
                done = true;
                return;
            }
            had[v] = true;
            ret.push_back(v);
            return;
        }
        if (!state[v]) {
            self(self, tree[v][0].first, tree[v][0].second);
        }
        else {
            self(self, tree[v][1].first, tree[v][1].second);
        }
        state[v] = state[v] ^ 1;
    };
    while (!done) dfs(dfs, root, false);
    return ret;
}
int build_top_down(int cnt_leaves, int shift, vector<int>& conns) {
    vector<vector<pair<int, bool>>> tree(4*cnt_leaves);
    int now = 0;
    vector<pair<int, bool>> inds;//ind, fake
    for (int i = 0; i < cnt_leaves; ++i) {
        inds.push_back({now++, false});
    }
    while (inds.size() > 1) {
        vector<pair<int, bool>> nxt_inds;
        for (int i = 0; i < inds.size(); i += 2) {
            tree[now].push_back(inds[i]);
            tree[now].push_back(inds[i+1]);
            nxt_inds.push_back({now++, false});
        }
        if (nxt_inds.size() != 1 && nxt_inds.size() % 2 != 0) {
            reverse(nxt_inds.begin(), nxt_inds.end());
            nxt_inds.push_back({now++, true});
            reverse(nxt_inds.begin(), nxt_inds.end());
        }
        inds.swap(nxt_inds);
    }
    int root = inds[0].first;
    vector<int> ret = get_ord(tree, root);
    vector<int> place(cnt_leaves);
    for (int i = 0; i < cnt_leaves; ++i) {
        place[ret[i]] = i;
    }
    shift += cnt_leaves;
    auto get = [&] (int ind) -> int {
        return shift+(-ind);
    };
    for (int i = 0; i < cnt_leaves; ++i) {
        if (conns[i] < 0) conns[i] = get(root);
    }
    auto fill_sw = [&] (auto&& self, int v, bool fake) -> void {
        if (fake) {
            sw[get(v)].first = get(root);
            sw[get(v)].second = get(root);
            return;
        }
        if (tree[v].front().first < cnt_leaves) {
            sw[shift+(-v)].first = conns[place[tree[v].front().first]];
            sw[shift+(-v)].second = conns[place[tree[v].back().first]];
            return;
        }
        sw[get(v)].first = get(tree[v].front().first);
        sw[get(v)].second = get(tree[v].back().first);
        self(self, tree[v].front().first, tree[v].front().second);
        self(self, tree[v].back().first, tree[v].back().second);
    };
    fill_sw(fill_sw, root, false);
    return get(root);
}


void create_circuit(int M, std::vector<int> A) {
    vector<int> C(M+1);
    vector<vector<int>> graph(M+1);
    const int n = A.size();
    graph[0].push_back(A[0]);
    for (int i = 0; i < n-1; ++i) {
        graph[A[i]].push_back(A[i+1]);
    }
    graph[A[n-1]].push_back(0);
    int nxt_free = -1;
    for (int i = 0; i <= M; ++i) {
        vector<int>& conns = graph[i];
        if (!conns.size()) {
            C[i] = 0;
            continue;
        }
        if (conns.size() == 1) {
            C[i] = conns.front();
            continue;
        }
        reverse(conns.begin(), conns.end());
        if (conns.size() % 2 != 0) conns.push_back(nxt_free);
        reverse(conns.begin(), conns.end());
        int root = build_top_down(conns.size(), nxt_free, conns);
        C[i] = root;
        nxt_free = root - 1;
    }
    vector<int> X, Y;
    for (int i = -1; i > nxt_free; --i) {
        X.push_back(sw[i].first);
        Y.push_back(sw[i].second);
    }
    answer(C, X, Y);
}

Compilation message

doll.cpp: In function 'int build_top_down(int, int, std::vector<int>&)':
doll.cpp:47:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |         for (int i = 0; i < inds.size(); i += 2) {
      |                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 23 ms 6960 KB Output is correct
3 Correct 14 ms 5724 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 10 ms 3772 KB Output is correct
6 Correct 25 ms 8540 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 23 ms 6960 KB Output is correct
3 Correct 14 ms 5724 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 10 ms 3772 KB Output is correct
6 Correct 25 ms 8540 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 74 ms 12240 KB Output is correct
9 Correct 57 ms 12416 KB Output is correct
10 Correct 115 ms 18808 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 23 ms 6960 KB Output is correct
3 Correct 14 ms 5724 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 10 ms 3772 KB Output is correct
6 Correct 25 ms 8540 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 74 ms 12240 KB Output is correct
9 Correct 57 ms 12416 KB Output is correct
10 Correct 115 ms 18808 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 161 ms 24908 KB Output is correct
15 Correct 73 ms 12924 KB Output is correct
16 Correct 158 ms 19648 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 116 ms 21712 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Partially correct 0 ms 348 KB Output is partially correct
2 Correct 163 ms 28484 KB Output is correct
3 Partially correct 154 ms 28444 KB Output is partially correct
4 Correct 332 ms 43140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 0 ms 348 KB Output is partially correct
2 Correct 163 ms 28484 KB Output is correct
3 Partially correct 154 ms 28444 KB Output is partially correct
4 Correct 332 ms 43140 KB Output is correct
5 Partially correct 171 ms 31080 KB Output is partially correct
6 Partially correct 186 ms 30868 KB Output is partially correct
7 Partially correct 163 ms 30900 KB Output is partially correct
8 Partially correct 168 ms 28856 KB Output is partially correct
9 Partially correct 141 ms 20084 KB Output is partially correct
10 Partially correct 190 ms 28732 KB Output is partially correct
11 Partially correct 135 ms 23228 KB Output is partially correct
12 Partially correct 85 ms 15564 KB Output is partially correct
13 Partially correct 121 ms 20416 KB Output is partially correct
14 Partially correct 104 ms 20452 KB Output is partially correct
15 Partially correct 110 ms 20332 KB Output is partially correct
16 Partially correct 3 ms 860 KB Output is partially correct
17 Partially correct 85 ms 15196 KB Output is partially correct
18 Partially correct 86 ms 14656 KB Output is partially correct
19 Partially correct 107 ms 14904 KB Output is partially correct
20 Partially correct 139 ms 22972 KB Output is partially correct
21 Partially correct 153 ms 22476 KB Output is partially correct
22 Partially correct 131 ms 22384 KB Output is partially correct