結果

問題 No.2918 Divide Applicants Fairly
ユーザー みうね
提出日時 2024-09-20 19:36:48
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 591 ms / 2,000 ms
コード長 1,006 bytes
コンパイル時間 3,331 ms
コンパイル使用メモリ 282,344 KB
実行使用メモリ 36,756 KB
最終ジャッジ日時 2024-10-07 21:06:14
合計ジャッジ時間 7,188 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int popcnt(int x) { return __builtin_popcount(x); }

// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }

template <class T, class S>
inline bool chmin(T &a, const S &b) {
    return (a > b ? a = b, 1 : 0);
}

int main() {
    int N;
    cin >> N;
    vector<int> R(N);
    for (int &r : R) cin >> r;
    if (N > 25) {
        cout << 0 << endl;
        return 0;
    }
    vector<int> comb;
    function<void(int, int)> exh = [&](int bit, int sum) {
        if (popcnt(bit) == N / 2) {
            comb.emplace_back(sum);
        } else {
            for (int i = topbit(bit) + 1; i < N - N / 2 + popcnt(bit) + 1; ++i) {
                exh(bit | (1 << i), sum + R[i]);
            }
        }
    };
    exh(0, 0);
    sort(comb.begin(), comb.end());
    int ans = 1 << 30;
    for (int i = 0; i < comb.size() - 1; ++i) {
        chmin(ans, abs(comb[i] - comb[i + 1]));
    }
    cout << ans << endl;
}
0