結果

問題 No.198 キャンディー・ボックス2
ユーザー willowoooowillowoooo
提出日時 2015-05-04 19:57:52
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 956 bytes
コンパイル時間 2,302 ms
コンパイル使用メモリ 75,148 KB
実行使用メモリ 12,296 KB
最終ジャッジ日時 2023-09-19 06:51:41
合計ジャッジ時間 5,471 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
using namespace std;

void solve198()
{
	long long B, N;
	
	cin >> B >> N;
	long long ans =1000000000000000000;
	vector<long long > BOX(N);

	long long val = 0;
	for (int i = 0; i < N; i++)
	{
		cin >> BOX[i];
		val += BOX[i];
	
	}

	val = val / N;
	vector<pair<long long,int> > goal(N);
	for (int i = 0; i < N; i++)
	{
		goal[i] = make_pair(abs(BOX[i] - val),i);
	}

	sort(BOX.begin(), BOX.end());
	reverse(BOX.begin(), BOX.end());
	
	sort(goal.begin(), goal.end());

	long long tmp = B;
	int i = 0;
	while (val != BOX[goal[1].second]){
		B = tmp;
		long long count = 0;
		val = goal[0].first + i;
		for (int i = 0; i < N; i++)
		{

			B += BOX[i] - val;
			count += abs(BOX[i] - val);

		}
		if (B >= 0){
		//	cout << val << endl;
		//	cout << count<<endl;
			ans = min(ans, count);
		}
		i++;
	}
	cout << ans << endl;
	return;
}
int main(){
	solve198();
	return 0;
}
0