結果

問題 No.198 キャンディー・ボックス2
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-24 01:25:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,140 bytes
コンパイル時間 804 ms
コンパイル使用メモリ 71,636 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-10 08:27:21
合計ジャッジ時間 1,822 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,388 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,384 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;
		// printf("sum %lld\n", sum);
	}

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

	sort(c.begin(), c.end());
	//下凸関数を三分探索
	// printf("sum %lld \n", sum);
	ll l = c[0];
	ll r = min(c[n - 1], sum / n) + 1;
	// ll r = c[n - 1];
	// printf("%lld %lld\n", l, r);
	while(r - l > 3){
		ll m1 = (l * 2 + r) / 3;
		ll m2 = (l + r * 2) / 3;
		// printf("%lld %lld\n", m1, m2);

		if(search(m1) > search(m2)){//左側が高い
			l = m1;
		}else{//右側が高い
			r = m2;
		}
	}
	// printf("%lld %lld\n", l, r);

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