結果

問題 No.198 キャンディー・ボックス2
ユーザー ty70ty70
提出日時 2015-05-28 17:56:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,920 bytes
コンパイル時間 1,236 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 17:09:14
合計ジャッジ時間 2,729 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1LL<<60

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll C[12];

int N;
ll f(ll base ){
	ll curr = 0LL;
	rep (i, N ) curr += abs (C[i] - base );

	return curr;
}

ll ternary_search (ll lo, ll hi ){
	int cnt = 1000;
	while (cnt-- ){
		ll cl = lo + (hi - lo )/3LL;
		ll ch = lo + 2LL*(hi - lo )/3LL; 
		if (f(cl) > f(ch) )
			lo = cl;
		else
			hi = ch;
	} // end while


	ll res = INF;
	for (ll base = lo; base <= hi; base++ ){
		ll curr = f(base );
		res = min (res, curr );
	} // end for

	return res;
}

ll linear_search (ll lo, ll hi ){

	ll res = INF;
	for (ll base = lo; base <= hi; base++ ){
		ll curr = 0LL;
		rep (i, N ){
			curr += abs(C[i] - base );
		} // end rep
		res = min (res, curr );
	} // end for

	return res;
}

int main()
{
	memset (C, 0LL, sizeof (C ) );
	ios_base::sync_with_stdio(0);
	ll B; cin >> B;
	cin >> N;
	ll sum = B;
	rep (i, N ) cin >> C[i], sum += C[i];

	ll max_base = sum/(ll)N;

	ll lo = 0;
	ll hi = max_base + 1LL;

	ll res1 = ternary_search (lo, hi );
	cout << res1 << endl;

//	ll res2 = linear_search (lo, hi );
//	cout << res1 << ' ' << res2 << endl;

	   
	return 0;
}
0