結果

問題 No.198 キャンディー・ボックス2
ユーザー hotpepsi
提出日時 2015-11-14 00:30:28
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 730 bytes
コンパイル時間 696 ms
コンパイル使用メモリ 59,884 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-13 16:31:17
合計ジャッジ時間 1,646 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>

using namespace std;
typedef long long LL;

int N;
LL B;
LL C[10];

LL cost(LL t) {
	LL cost = 0;
	for (int i = 0; i < N; ++i) {
		cost += abs(t - C[i]);
	}
	return cost;
}

int main(int argc, char *argv[]) {
	cin >> B;
	cin >> N;
	for (int i = 0; i < N; ++i) {
		cin >> C[i];
	}
	LL m = accumulate(C, C + N, B) / N;
	LL left = *min_element(C, C + N);
	LL right = *max_element(C, C + N);
	for (LL a = 0; a < 10000; ++a) {
		if (cost((left * 2 + right) / 3) <= cost((left + right * 2) / 3)) {
			right = (left + right * 2) / 3;
		} else {
			left = (left * 2 + right) / 3;
		}
	}
	LL ans = cost((left + right) / 2);
	cout << ans << endl;
	return 0;
}
0