結果

問題 No.198 キャンディー・ボックス2
ユーザー ctyl_0ctyl_0
提出日時 2015-10-05 02:19:47
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,572 bytes
コンパイル時間 804 ms
コンパイル使用メモリ 86,300 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-27 02:04:30
合計ジャッジ時間 2,843 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
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 2 ms
4,376 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 WA -
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 AC 2 ms
4,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; // 箱の数
    int 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 mid = low + 1;
    
    lcnt = 0;
    ll mcnt = 0;
    hcnt = 0;
    rep(i, N){
        lcnt += abs(C[i] - low);
        mcnt += abs(C[i] - mid);
        hcnt += abs(C[i] - high);
    }
    
    output(min(lcnt, min(mcnt, hcnt)), 0);
    
    return 0;
}
0