結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#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 B, N;
    cin >> B >> N;
    vector<long> C;
    REP(i,N) {
        long c;
        cin >> c;
        C.push_back(c);
    }

     long long l = 0, r = 1000000000000000000;
    while (r - l > 2) {
         long long lm, rm;
        lm = (l + r) / 3;
        rm = (l + r) / 3 * 2;
        if (lm * N - accumulate(C.begin(), C.end(), 0) > B) {
            r = lm;
        } else if (rm * N - accumulate(C.begin(), C.end(), 0) > B) {
            r = rm;
        } else if (accumulate(C.begin(), C.end(), 0, [lm]( long long acc,  long long c) { return acc + abs(c - lm); }) >=
                accumulate(C.begin(), C.end(), 0, [rm]( long long acc,  long long c) { return acc + abs(c - rm); })) {
            r = rm;
        } else {
            l = lm;
        }
    }
    cout << accumulate(C.cbegin(), C.cend(), 0, [r, l]( long long acc,  long long c) { return acc + abs((r + l) / 2 - c); }) << endl;

    return 0;
}
0