답안 #197721

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
197721 2020-01-22T13:42:00 Z dolphingarlic ČVENK (COI15_cvenk) C++14
60 / 100
3000 ms 91288 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;

ll tot_dist = 0, ans = LLONG_MAX;

struct Node {
    ll x, y, l;
    Node *c[2];

    Node(ll X = 0, ll Y = 0): x(X), y(Y), l(0) {
        c[0] = c[1] = NULL;
    }

    Node* insert(Node *ins, int child) {
        l++;
        if (!c[child]) {
            c[child] = ins;
            return ins;
        }
        if (ins->x < c[child]->x || ins->y < c[child]->y) {
            ins->c[child] = c[child];
            ins->l += c[child]->l;
            c[child] = ins;
            return ins;
        }
        if (ins->x > c[child]->x || ins->y > c[child]->y) 
            return c[child]->insert(ins, child);
        return c[child];
    }
};

int n;
Node *root = new Node();

void dfs(Node *curr) {
    ans = min(ans, tot_dist);
    for (int i = 0; i < 2; i++) {
        if (curr->c[i]) {
            ll dx = curr->x - curr->c[i]->x + curr->y - curr->c[i]->y;
            tot_dist += dx * (2 * curr->c[i]->l - n);
            dfs(curr->c[i]);
            tot_dist -= dx * (2 * curr->c[i]->l - n);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        Node *curr = root;
        ll x, y, cx = 0, cy = 0;
        cin >> x >> y;
        tot_dist += x + y;
        for (int j = 30; ~j; j--) {
            if (x & (1 << j)) {
                cx |= (1 << j);
                curr = curr->insert(new Node(cx, cy), 0);
            }
            if (y & (1 << j)) {
                cy |= (1 << j);
                curr = curr->insert(new Node(cx, cy), 1);
            }
        }
        curr->l++;
    }
    dfs(root);
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 25436 KB Output is correct
2 Correct 208 ms 25384 KB Output is correct
3 Correct 73 ms 16588 KB Output is correct
4 Correct 65 ms 15224 KB Output is correct
5 Correct 75 ms 16760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2462 ms 91288 KB Output is correct
2 Correct 2491 ms 91240 KB Output is correct
3 Execution timed out 3029 ms 7436 KB Time limit exceeded
4 Halted 0 ms 0 KB -