結果

問題 No.198 キャンディー・ボックス2
ユーザー ty70ty70
提出日時 2015-05-28 19:08:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,478 bytes
コンパイル時間 940 ms
コンパイル使用メモリ 89,752 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-10 07:33:57
合計ジャッジ時間 2,140 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 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,380 KB
testcase_23 AC 2 ms
4,504 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
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 B ){
	ll curr = 0LL;
	rep (i, N ) {
		if (C[i] - base >= 0 ){
			curr += C[i] - base;
			B += C[i] -  base;
		}else{
			if (B >= base - C[i] ){
				curr += base - C[i];
				B -= base - C[i];
			}else{
				curr = INF;
				break;
			} // end if
		} // end if
	} // end rep

	return curr;
}

ll ternary_search (ll lo, ll hi, ll B ){

	if (N == 1 ) return 0LL;

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


	ll res = INF;
	for (ll base = max(lo-100LL, 0LL ); base <= hi; base++ ){
		ll curr = f(base, B );
		res = min (res, curr );
	} // end for

	return res;
}

ll linear_search (ll lo, ll hi, ll B ){

	if (N == 1 ) return 0LL;


	ll res = INF;
	for (ll base = lo; base <= hi; base++ ){
		ll curr = 0LL;
		ll b = B;
		rep (i, N ){
			if (C[i] - base >= 0 ){
				curr += (C[i] - base );
				b += (C[i] - base);
			}else{
				if (b >= base - C[i] ){
					curr += (base - C[i] );
					b -= (base - C[i] );
				}else{
					curr = INF;
					break;
				} // end if
			} // edn if
		} // 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];

	sort (C, C+N, greater<ll>() );
	ll max_base = sum/(ll)N;

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

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

//	ll res2 = linear_search (lo, hi, B );
//	cout << res2 << endl;
	   
	return 0;
}
0