結果

問題 No.2918 Divide Applicants Fairly
ユーザー けいと
提出日時 2024-10-09 01:00:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,718 bytes
コンパイル時間 3,775 ms
コンパイル使用メモリ 282,068 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-10-09 01:00:35
合計ジャッジ時間 5,520 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    ull n;
    cin >> n;
    vector<ull> r(n);
    for(auto &x : r) cin >> x;
    
    sort(r.begin(), r.end());
    r.push_back(0);
    
    vector<ull> sum(n+1, 0);
    for(ull i = n; i > 0; --i){
        sum[i-1] = r[i-1] + sum[i];
    }
    
    ull m = n / 2;
    ull size_val = sum[n - m];
    const ull w = 64;
    
    ull dp_size = (size_val / w) + 1;
    vector<ull> dp(dp_size, 0);
    
    ull bit;
    if(m >= 64){
        bit = ~0ULL;
    }
    else{
        bit = (1ULL << m) - 1;
    }
    
    ull s = sum[0] - sum[m];
    
    while(bit < (1ULL << n)){
        ull dp_index = s / w;
        ull dp_bit = s % w;
        if( (dp[dp_index] >> dp_bit) & 1 ){
            cout << "0\n";
            return 0;
        }
        dp[dp_index] |= (1ULL << dp_bit);
        
        if(bit == 0){
            break;
        }
        
        int z = __builtin_ctzll(bit);
        ull shifted = bit >> z;
        int o = __builtin_ctzll(~shifted);
        
        s += r[z + o];
        s -= (sum[z] - sum[z + o]);
        s += (sum[0] - sum[o - 1]);
        
        bit = (bit + (1ULL << z)) | ((1ULL << (o - 1)) - 1);
    }
    
    ull ans = ULLONG_MAX;
    ull up = ULLONG_MAX;
    
    for(ull i = dp_size; i-- > 0; ){
        ull current = dp[i];
        while(current > 0){
            int pos = 63 - __builtin_clzll(current);
            ull u = i * w + pos;
            if(up != ULLONG_MAX){
                ans = min(ans, up - u);
            }
            up = u;
            current &= ~(1ULL << pos);
        }
    }
    
    cout << ans << "\n";
}
0