結果

問題 No.2918 Divide Applicants Fairly
ユーザー HIcoder
提出日時 2024-10-10 21:47:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 595 ms / 2,000 ms
コード長 1,168 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 121,552 KB
最終ジャッジ日時 2025-02-24 17:22:57
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
#include<unordered_map>
#include<bitset>
#include<numeric>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<int,ll> P;
typedef pair<int,P> PP; 
const ll MOD=998244353;

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

int top_bit(int x){
    if(x==0) return -1;
    return 31-__builtin_clz(x);
}

int main(){
    int N;
    cin>>N;
    vector<ll> R(N);
    for(int i=0;i<N;i++){
        cin>>R[i];
    }
    if(N>25){
        cout<<0<<endl;
        return 0;
    }

    //N<=25

    vector<int> comb;
    auto exh=[&](auto f,int bit,int sum){
        if(popcount(bit)==N/2){
            comb.push_back(sum);
            return;
        }
        for(int i=top_bit(bit)+1;i<N-N/2+popcount(bit)+1;i++){
            f(f,bit|(1<<i),sum+R[i]);
        }
    };

    exh(exh,0,0);
    sort(comb.begin(),comb.end());
    int ans=inf;
    for(int i=0;i+1<comb.size();i++){
        ans=min(ans,abs(comb[i]-comb[i+1]));
    }
    cout<<ans<<endl;
}
0