結果
| 問題 | 
                            No.130 XOR Minimax
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2017-07-28 03:19:06 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 90 ms / 5,000 ms | 
| コード長 | 2,140 bytes | 
| コンパイル時間 | 1,864 ms | 
| コンパイル使用メモリ | 178,748 KB | 
| 実行使用メモリ | 11,236 KB | 
| 最終ジャッジ日時 | 2024-09-12 22:48:49 | 
| 合計ジャッジ時間 | 3,868 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
ソースコード
#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;
}