結果

問題 No.198 キャンディー・ボックス2
ユーザー ctyl_0ctyl_0
提出日時 2015-10-05 02:22:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,664 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 84,416 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 01:38:14
合計ジャッジ時間 1,297 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;

using namespace std;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

int main() {
    
    // source code
    int B = inputValue(); // 手持ち
    ll N; // 箱の数
    ll num = B; // 合計の数
    cin >> N;
    vector<int> C(N);
    rep(i, N){
        C[i] = inputValue(); // 各箱の中の数
        num += C[i];
    }
    
    ll low = 0;
    ll high = num / N;
    
    ll lcnt = 0;
    ll hcnt = 0;
    
    while (low + 2 < high) {
        ll lmid = (2 * low + high) / 3;
        ll hmid = (low + 2 * high) / 3;
        
        lcnt = 0;
        hcnt = 0;
        
        rep(i, N){
            lcnt += abs(C[i] - lmid);
            hcnt += abs(C[i] - hmid);
        }
        
        if (lcnt > hcnt) {
            low = lmid;
        }
        else{
            high = hmid;
        }
    }
    ll lmid = low + 1;
    ll hmid = low + 2;
    
    lcnt = 0;
    ll lmcnt = 0;
    ll hmcnt = 0;
    hcnt = 0;
    rep(i, N){
        lcnt += abs(C[i] - low);
        lmcnt += abs(C[i] - lmid);
        hmcnt += abs(C[i] - hmid);
        hcnt += abs(C[i] - high);
    }
    
    output(min(min(lcnt, lmcnt), min(hmcnt, hcnt)), 0);
    
    return 0;
}
0