結果

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

ソースコード

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) / w;
    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;
        }
        
        // Find trailing zeros
        int z = __builtin_ctzll(bit);
        
        // Find trailing ones after z
        ull shifted = bit >> z;
        int o = __builtin_ctzll(~shifted);
        // Alternatively, count trailing ones
        // int o = 0;
        // while((shifted >> o) & 1){
        //     o++;
        // }
        
        // Update sum 's'
        s += r[z + o];
        s -= (sum[z] - sum[z + o]);
        if(o == 0){
            s += sum[0];
        }
        else{
            s += (sum[0] - sum[o -1]);
        }
        
        // Update bitmask
        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){
            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