結果

問題 No.198 キャンディー・ボックス2
ユーザー kpinkcatkpinkcat
提出日時 2023-11-06 00:12:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,481 bytes
コンパイル時間 2,641 ms
コンパイル使用メモリ 206,844 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-11-06 00:12:30
合計ジャッジ時間 3,782 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 2 ms
4,372 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 2 ms
4,372 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 2 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<list>
#include<queue>
#include<math.h>
#include<bitset>
using ll = long long;
using namespace std;

ll solve(vector<int> &v, ll bt, ll t1, ll t2 = -1){
    ll ret = 1e10;
    if (t2 == -1) t2 = t1;
    if (t1 > t2) swap(t1, t2);
    for (ll i = 0; i < 2; i++){
        if (i == 1) t1 = t2;  
        ll ans = 0, b = bt;
        for (int j = 0; j < v.size(); j++){
            if (v[j] > t1) {
                b += (v[j] - t1);
                ans += (v[j] - t1);
            }
        }
        for (int j = 0; j < v.size(); j++){
            if (v[j] < t1) {
                if (b >= (t1 - v[j])) {
                    b -= (t1 - v[j]);
                    ans += (t1 - v[j]);
                }
            }
        }
        ret = min(ret, ans);
    }
    return ret;
}

int main(){
    ll b, n, t1, t2, ave = 0, ans = 0;
    cin >> b >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        ave += v[i];
    }
    ave += b;
    ave /= n;
    sort(v.begin(), v.end());
    if (n%2) {
        t1 = v[n/2];
        if (ave < t1) t1 = ave;
        ans = solve(v, b, t1);
    } else {
        t2 = v[n/2];
        t1 = v[n/2 - 1];
        if (ave < t1) {
            t1 = ave;
            t2 = ave;
        }
        if (ave < t2) t2 = ave;
        ans = solve(v, b, t1, t2);
    }
    cout << ans << endl;
}
0