답안 #204416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
204416 2020-02-25T17:21:36 Z stefdasca ČVENK (COI15_cvenk) C++14
100 / 100
200 ms 93816 KB
#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;

struct nod
{
    ll x, y, sz;
    nod *fii[2];

    nod(ll X = 0, ll Y = 0): x(X), y(Y), sz(0)
    {
        fii[0] = fii[1] = NULL;
    }

    nod* add(nod *tbi, int kid)
    {
        ++sz;
        if(!fii[kid])
        {
            fii[kid] = tbi;
            return tbi;
        }
        if(tbi -> x < fii[kid] -> x || tbi -> y < fii[kid] -> y)
        {
            tbi -> fii[kid] = fii[kid];
            tbi -> sz += fii[kid] -> sz;
            fii[kid] = tbi;
            return tbi;
        }
        if (tbi -> x > fii[kid] -> x || tbi -> y > fii[kid] -> y)
            return fii[kid] -> add(tbi, kid);
        return fii[kid];
    }
};
nod *rad = new nod();
int n;
ll sum, minans = (1LL<<60);
pair<int, int> pct[100002];

bool cmp(pair<int, int> a, pair<int, int> b)
{
    return (a.fi + a.se) > (b.fi + b.se);
}

void dfs(nod *cur)
{
    minans = min(minans, sum);
    for(int i = 0; i <= 1; i++)
        if(cur -> fii[i])
        {
            ll dx = (cur -> x) - (cur -> fii[i] -> x) + (cur -> y) - (cur -> fii[i] -> y);
            sum += dx * (2 * cur -> fii[i] -> sz - n);
            dfs(cur -> fii[i]);
            sum -= dx * (2 * cur -> fii[i] -> sz - n);
        }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i = 1; i <= n; ++i)
    {
        cin >> pct[i].fi >> pct[i].se;
        sum += pct[i].fi + pct[i].se;
    }
    sort(pct + 1, pct + n + 1, cmp);
    for(int i = 1; i <= n; ++i)
    {
        nod *cur = rad;
        ll cx = 0, cy = 0;
        for(int j = 29; j >= 0; --j)
        {
            if(pct[i].fi & (1<<j))
            {
                cx |= (1<<j);
                cur = cur -> add(new nod(cx, cy), 0);
            }
            else
                if(pct[i].se & (1<<j))
                {
                    cy |= (1<<j);
                    cur = cur -> add(new nod(cx, cy), 1);
                }
        }
        cur -> sz++;
    }
    dfs(rad);
    cout << minans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 26872 KB Output is correct
2 Correct 60 ms 26872 KB Output is correct
3 Correct 47 ms 17840 KB Output is correct
4 Correct 45 ms 16632 KB Output is correct
5 Correct 49 ms 18040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 93816 KB Output is correct
2 Correct 200 ms 93816 KB Output is correct
3 Correct 164 ms 89592 KB Output is correct
4 Correct 155 ms 79480 KB Output is correct
5 Correct 159 ms 76920 KB Output is correct