結果

問題 No.198 キャンディー・ボックス2
ユーザー agatanagatan
提出日時 2015-05-04 23:07:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,094 bytes
コンパイル時間 496 ms
コンパイル使用メモリ 64,056 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-19 06:55:44
合計ジャッジ時間 1,708 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include <climits>

#define REP(i, n) for(int i=0;i<(n);i++)

using namespace std;


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    long long B, N;
    cin >> B >> N;
    long long maxt = 0, mint = 0;
    vector<long long> C;
    REP(i,N) {
        long long c;
        cin >> c;
        C.push_back(c);
        maxt += c;
    }
    maxt += B;
    maxt /= N;

    while (maxt - mint > 2) {
        long long len = maxt - mint + 1;
        len /= 3;
        long long lm = mint + len;
        long long rm = mint + len + len;

        long long lv = 0, rv = 0;
        for(auto c: C) {
            lv += abs(c - lm);
            rv += abs(c = rm);
        }
        if (lv > rv) {
            mint = lv;
        } else {
            maxt = rv;
        }
    }
    long long ans = LLONG_MAX;
    for (long long i = mint; i <= maxt; ++i) {
        long long r = 0;
        for (auto c: C) {
            r += abs(c - i);
        }
        ans = min(ans, r);
    }
    cout << ans << endl;

    return 0;
}
0