結果

問題 No.198 キャンディー・ボックス2
ユーザー @abcde@abcde
提出日時 2019-03-17 13:14:11
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,178 bytes
コンパイル時間 1,381 ms
コンパイル使用メモリ 151,440 KB
実行使用メモリ 8,756 KB
最終ジャッジ日時 2023-09-20 17:50:50
合計ジャッジ時間 7,177 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
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 <bits/stdc++.h>
using namespace std;
typedef long long LL;

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