結果
問題 | No.2918 Divide Applicants Fairly |
ユーザー |
![]() |
提出日時 | 2024-10-09 00:37:28 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 8 ms / 2,000 ms |
コード長 | 1,818 bytes |
コンパイル時間 | 5,631 ms |
コンパイル使用メモリ | 281,528 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-09 00:37:36 |
合計ジャッジ時間 | 6,813 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); size_t n; cin >> n; vector<size_t> r(n); for(auto &x : r) cin >> x; sort(r.begin(), r.end()); r.push_back(0); vector<size_t> sum = r; for(int i = n -1; i >=0; --i){ sum[i] += sum[i+1]; } size_t m = n / 2; size_t size_val = sum[n - m]; size_t w = 8 * sizeof(size_t); size_t dp_size = size_val / w + 1; vector<size_t> dp(dp_size, 0); size_t bit = (m >= sizeof(size_t)*8) ? (size_t)(-1) : ((1ULL << m) -1); size_t s = sum[0] - sum[m]; while(bit < (1ULL << n)){ size_t dp_index = s / w; size_t 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); size_t shifted = bit >> z; int o = 0; while(shifted & 1){ o++; shifted >>=1; } s += r[z + o]; s -= (sum[z] - sum[z + o]); s += (sum[0] - sum[o -1]); bit = (bit + (1ULL << z)) | ((1ULL << (o -1)) -1); } size_t ans = numeric_limits<size_t>::max(); size_t up = numeric_limits<size_t>::max(); for(int i = dp.size()-1; i >=0; --i){ size_t current = dp[i]; while(current >0){ int pos = 63 - __builtin_clzll(current); size_t u = i * w + pos; if(up != numeric_limits<size_t>::max()){ ans = min(ans, up - u); } up = u; current &= ~(1ULL << pos); } } cout << ans << "\n"; }