結果

問題 No.130 XOR Minimax
ユーザー PachicobuePachicobue
提出日時 2017-07-28 03:19:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 2,140 bytes
コンパイル時間 1,822 ms
コンパイル使用メモリ 177,288 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2023-10-10 00:20:00
合計ジャッジ時間 4,099 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
6,456 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 27 ms
10,936 KB
testcase_05 AC 70 ms
11,028 KB
testcase_06 AC 71 ms
11,084 KB
testcase_07 AC 89 ms
11,128 KB
testcase_08 AC 89 ms
11,136 KB
testcase_09 AC 14 ms
4,392 KB
testcase_10 AC 20 ms
5,068 KB
testcase_11 AC 63 ms
9,764 KB
testcase_12 AC 7 ms
4,348 KB
testcase_13 AC 52 ms
8,480 KB
testcase_14 AC 86 ms
9,776 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 60 ms
7,636 KB
testcase_17 AC 63 ms
7,984 KB
testcase_18 AC 68 ms
8,212 KB
testcase_19 AC 80 ms
9,224 KB
testcase_20 AC 39 ms
6,132 KB
testcase_21 AC 85 ms
9,904 KB
testcase_22 AC 19 ms
4,644 KB
testcase_23 AC 6 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

ll rec(const vector<ll>& a, const vector<vector<bool>>& digit, const ll x, const ll d, const ll l, const ll r)  // suffix = x, bit = d, range = a[l]~a[r-1]
{
    if (d < 0) {
        return 0;
    }
    if (l >= r - 1) {
        return 0;
    }
    const ll y = x + (1LL << d);  // dth-bit of object = 1
    const ll z = x;               // dth-bit of object = 0
    const ll yl = lower_bound(a.begin(), a.end(), y) - a.begin();
    const ll yu = upper_bound(a.begin(), a.end(), y + (1LL << d) - 1) - a.begin();
    const ll zl = lower_bound(a.begin(), a.end(), z) - a.begin();
    const ll zu = upper_bound(a.begin(), a.end(), z + (1LL << d) - 1) - a.begin();
    if (yu == yl) {
        return rec(a, digit, z, d - 1, zl, zu);
    }
    if (zu == zl) {
        return rec(a, digit, y, d - 1, yl, yu);
    }
    return (1LL << d) + min(rec(a, digit, y, d - 1, yl, yu), rec(a, digit, z, d - 1, zl, zu));
}

int main()
{
    ll N;
    cin >> N;
    vector<ll> a(N);
    ll max_digits = 0;
    for (ll i = 0; i < N; i++) {
        cin >> a[i];
        max_digits = max(max_digits, (ll)log2(a[i] - 1) + 1);
    }
    max_digits++;
    sort(a.begin(), a.end());

    vector<vector<bool>> digit(N, vector<bool>(max_digits, false));
    for (ll i = 0; i < N; i++) {
        ll num = a[i];
        for (ll j = 0; j < max_digits; j++) {
            digit[i][j] = (bool)(num % 2);
            num /= 2;
        }
    }

    cout << rec(a, digit, 0, max_digits - 1, 0, N) << endl;

    return 0;
}
0