結果

問題 No.198 キャンディー・ボックス2
ユーザー @abcde@abcde
提出日時 2019-03-17 14:41:44
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,122 bytes
コンパイル時間 1,169 ms
コンパイル使用メモリ 146,948 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-20 20:11:23
合計ジャッジ時間 2,571 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main() {
    
    // 1. 入力情報取得.
    LL B, N;
    cin >> B >> N;
    LL C[N];
    for(int i = 0; i < N; i++) cin >> C[i];
    sort(C, C + N);
    
    // 2. キャンディーの総数を計算.
    LL candy = 0, ans = 1e11;
    for(int i = 0; i < N; i++) candy += C[i];
    
    // 3. キャンディーの配分個数を二分探索.
    // 配分個数: (最小): l, (最大): h.
    LL l = 0, h = max(1LL, (candy + B) / N);
    while(l < h){
        // 操作回数: (最小): ol, (最大): oh.
        LL ol = 0, oh = 0;
        for(int i = 0; i < N; i++) ol += abs(C[i] - l), oh += abs(C[i] - h);
        // cout << "l=" << l << " ol=" << ol << " h=" << h << " oh=" << oh << endl;
        // キャンディーの配分個数を更新.
        if(ol < oh){
            if(ans >= ol && l * N <= candy) ans = ol;
            h = (h + l) / 2, l++;
        }else{
            if(ans >= oh && h * N <= candy) ans = oh;
            l = (h + l) / 2, h--;
        }
    }
    
    // 3. 出力.
    cout << ans << endl;
    return 0;
    
}
0