結果

問題 No.198 キャンディー・ボックス2
ユーザー kobajinkobajin
提出日時 2019-12-08 02:06:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,011 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 166,760 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-27 15:01:05
合計ジャッジ時間 2,686 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
    return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main() {
    ll b, n; cin >> b >> n;
    ll sum = b;
    vector<ll> c(n);

    REP(i, n) {
        cin >> c[i];
        sum += c[i];
    }

    auto f = [&](ll x) {
        ll cost = 0;

        REP(i, n) cost += abs(c[i] - x);

        return cost;
    };

    ll l = 0, r = sum / n + 1;
    while (r - l > 3) {
        ll t1 = (2 * l + r) / 3, t2 = (l + r * 2) / 3;

        if (f(t1) >= f(t2)) {
            l = t1;
        } else {
            r = t2;
        }
    }

    ll ans = numeric_limits<ll>::max();
    FOR(i, l, r) ans = min(ans, f(i));

    cout << ans << endl;

    return 0;
}
0