結果

問題 No.198 キャンディー・ボックス2
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-24 02:04:43
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 915 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 70,644 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 06:20:17
合計ジャッジ時間 2,217 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 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 2 ms
4,380 KB
testcase_19 WA -
testcase_20 AC 2 ms
4,380 KB
testcase_21 WA -
testcase_22 AC 1 ms
4,384 KB
testcase_23 WA -
testcase_24 AC 1 ms
4,376 KB
testcase_25 WA -
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 WA -
testcase_29 AC 1 ms
4,380 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 = c[0];
	ll r = c[n - 1] + 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