結果
| 問題 |
No.2918 Divide Applicants Fairly
|
| ユーザー |
けいと
|
| 提出日時 | 2024-10-09 00:52:28 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 61 |
ソースコード
#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";
}
けいと