結果

問題 No.2918 Divide Applicants Fairly
ユーザー けいとけいと
提出日時 2024-10-09 00:52:28
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,792 bytes
コンパイル時間 4,962 ms
コンパイル使用メモリ 281,176 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-09 00:52:36
合計ジャッジ時間 6,852 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 3 ms
5,248 KB
testcase_04 AC 3 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 3 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 3 ms
5,248 KB
testcase_21 AC 3 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 3 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 2 ms
5,248 KB
testcase_28 AC 3 ms
5,248 KB
testcase_29 AC 2 ms
5,248 KB
testcase_30 AC 2 ms
5,248 KB
testcase_31 AC 2 ms
5,248 KB
testcase_32 AC 3 ms
5,248 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 2 ms
5,248 KB
testcase_36 AC 9 ms
5,248 KB
testcase_37 AC 6 ms
5,248 KB
testcase_38 AC 4 ms
5,248 KB
testcase_39 AC 3 ms
5,248 KB
testcase_40 AC 3 ms
5,248 KB
testcase_41 AC 3 ms
5,248 KB
testcase_42 AC 3 ms
5,248 KB
testcase_43 AC 2 ms
5,248 KB
testcase_44 AC 3 ms
5,248 KB
testcase_45 AC 3 ms
5,248 KB
testcase_46 AC 4 ms
5,248 KB
testcase_47 AC 2 ms
5,248 KB
testcase_48 AC 2 ms
5,248 KB
testcase_49 AC 6 ms
5,248 KB
testcase_50 AC 2 ms
5,248 KB
testcase_51 AC 3 ms
5,248 KB
testcase_52 AC 2 ms
5,248 KB
testcase_53 AC 2 ms
5,248 KB
testcase_54 AC 2 ms
5,248 KB
testcase_55 AC 3 ms
5,248 KB
testcase_56 AC 3 ms
5,248 KB
testcase_57 AC 2 ms
5,248 KB
testcase_58 AC 3 ms
5,248 KB
testcase_59 AC 3 ms
5,248 KB
testcase_60 AC 2 ms
5,248 KB
testcase_61 AC 2 ms
5,248 KB
testcase_62 AC 2 ms
5,248 KB
testcase_63 AC 3 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef uint64_t 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 = UINT64_MAX;
    }
    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]);
        if(o == 0){
            s += sum[0];
        }
        else{
            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