結果

問題 No.198 キャンディー・ボックス2
ユーザー 0w10w1
提出日時 2016-11-27 17:23:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,068 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 78,868 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 08:27:43
合計ジャッジ時間 2,071 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
const ll INF = 1e18;

ll b;
vector<ll> c;
int n;

ll search(ll m){
    ll cnt = 0;
    rep(i, n){
        cnt += abs(m - c[i]);
    }
    return cnt;
}

int main(void){
    cin >> b >> n;
    ll sum = b;
    rep(i, n){
        ll t; cin >> t;
        c.push_back(t);
        sum += t;
    }

    if(n == 1){
        printf("0\n");
        return 0;
    }

    sort(c.begin(), c.end());
    //下凸関数を三分探索
    ll l = 0;
    ll r = ( int ) 1e9 + 1;
    while(r - l > 3){
        ll m1 = (l * 2 + r) / 3;
        ll m2 = (l + r * 2) / 3;

        if(search(m1) > search(m2)){//左側が高い
            l = m1;
        }else{//右側が高い
            r = m2;
        }
    }

    ll ans = INF;
    for (int i = l; i < r; ++i){
        ans = min(ans, search(i));
    }
    printf("%lld\n", ans);
    return 0;
}
0