結果

問題 No.198 キャンディー・ボックス2
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-28 23:49:53
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 847 bytes
コンパイル時間 1,178 ms
コンパイル使用メモリ 146,436 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-18 14:16:21
合計ジャッジ時間 2,367 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

 
vector<int> C;
int B, N;
long long ans;

long long calc(long long a){
	long long nokori = B;
	long long temp = 0;
	for (int i = 0; i < N; i++)
	{
		temp += abs(a - C[i]);
		nokori += C[i] - a;
	}
	if (nokori < 0) return (long long) 1e18;
	ans = min(temp, ans);
	return temp;
}

int main() {
	cin >> B;
	cin >> N;
	C = vector<int>(N);
	for (int i = 0; i < N; i++)
	{
		cin >> C[i];
	}
	ans = (long long) 1e18;
	long long low = 0;
	long long high = (int)1e9 + 2;
	while (high - low > 10){
		long long lowmid = (low + low + high) / 3;
		long long highmid = (low + high + high) / 3;
		cout << lowmid << " " << highmid << endl;
		if (calc(lowmid) <= calc(highmid)) high = highmid;
		else low = lowmid;
	}

	for (int i = max(0ll , low - 100); i < low + 100; i++)
	{
		calc(i);
	}
	cout << ans << endl;
}
0