結果

問題 No.2918 Divide Applicants Fairly
ユーザー けいとけいと
提出日時 2024-10-09 00:28:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,180 bytes
コンパイル時間 1,894 ms
コンパイル使用メモリ 135,236 KB
実行使用メモリ 22,112 KB
最終ジャッジ日時 2024-10-09 00:29:11
合計ジャッジ時間 9,927 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,496 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 3 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 1,159 ms
5,452 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 365 ms
5,248 KB
testcase_13 AC 6 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 364 ms
5,248 KB
testcase_18 WA -
testcase_19 AC 3 ms
5,248 KB
testcase_20 WA -
testcase_21 AC 365 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 1,147 ms
5,320 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
5,248 KB
testcase_27 WA -
testcase_28 AC 2 ms
5,248 KB
testcase_29 AC 6 ms
5,248 KB
testcase_30 WA -
testcase_31 AC 15 ms
5,248 KB
testcase_32 WA -
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 2 ms
5,248 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <climits>
#include <bitset>

using namespace std;

typedef long long ll;

int N;
vector<int> R;

int main() {
    cin >> N;
    R.resize(N);
    ll total_sum = 0;
    for (int i = 0; i < N; ++i) {
        cin >> R[i];
        total_sum += R[i];
    }

    ll min_diff = LLONG_MAX;

    if (N <= 20) {
        int total_N = N;
        vector<int> indices(N);
        for (int i = 0; i < N; ++i) {
            indices[i] = i;
        }

        for (int total_k = 2; total_k <= N; total_k += 2) {
            vector<vector<int>> subsets;
            vector<bool> select(N, false);
            for (int i = 0; i < total_k; ++i) {
                select[i] = true;
            }
            do {
                vector<int> subset_indices;
                for (int i = 0; i < N; ++i) {
                    if (select[i]) {
                        subset_indices.push_back(i);
                    }
                }
                subsets.push_back(subset_indices);
            } while (prev_permutation(select.begin(), select.end()));

            for (auto& subset : subsets) {
                int k = total_k / 2;
                int subset_size = subset.size();
                vector<int> subset_ratings;
                for (int idx : subset) {
                    subset_ratings.push_back(R[idx]);
                }
                int m = subset_size;
                vector<bool> team_select(m, false);
                for (int i = 0; i < k; ++i) {
                    team_select[i] = true;
                }
                ll total_selected_sum = 0;
                for (int r : subset_ratings) {
                    total_selected_sum += r;
                }
                do {
                    ll team1_sum = 0;
                    for (int i = 0; i < m; ++i) {
                        if (team_select[i]) {
                            team1_sum += subset_ratings[i];
                        }
                    }
                    ll team2_sum = total_selected_sum - team1_sum;
                    ll diff = abs(team1_sum - team2_sum);
                    if (diff < min_diff) {
                        min_diff = diff;
                    }
                } while (prev_permutation(team_select.begin(), team_select.end()));
            }
        }
    } else {
        int total_k = N;
        if (total_k % 2 != 0) {
            total_k = N - 1;
        }
        vector<pair<int, int>> rating_with_index;
        for (int i = 0; i < N; ++i) {
            rating_with_index.emplace_back(R[i], i);
        }
        sort(rating_with_index.begin(), rating_with_index.end(), greater<pair<int, int>>());

        ll team1_sum = 0;
        ll team2_sum = 0;
        for (int i = 0; i < total_k; ++i) {
            if (i % 2 == 0) {
                team1_sum += rating_with_index[i].first;
            } else {
                team2_sum += rating_with_index[i].first;
            }
        }
        ll diff = abs(team1_sum - team2_sum);
        if (diff < min_diff) {
            min_diff = diff;
        }
    }

    cout << min_diff << endl;

    return 0;
}
0