結果

問題 No.198 キャンディー・ボックス2
ユーザー ramia777ramia777
提出日時 2022-06-07 11:47:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,780 bytes
コンパイル時間 924 ms
コンパイル使用メモリ 98,604 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 03:49:02
合計ジャッジ時間 2,121 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

//Yukicoder
//Q198 candy box
//




//二次元可変配列
//vector <vector <int>> mass;
//vector <vector <int>> memo;
//vector <int> memo;


//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>//list
#include <queue> //queue
#include <set> //連想コンテナ(要素が自動でソートされる)、要素1つ(Key)、keyの重複ない、O(logN)
#include <map> //連想コンテナ(要素が自動でソートされる)、要素2つ(Key,data)、keyの重複ない、dataは重複あり、O(logN)
#include <unordered_set> //hash、O(1)
#include <unordered_map> //hash、O(1)
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <climits>
//#include <bits/stdc++.h>

using namespace std;

#define FOR(x,to) for(x=0;x<to;x++)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define FMAX(a,b)  ((a)>(b)?(a):(b))
#define FMIN(a,b)  ((a)<(b)?(a):(b))
#define FCEIL(a,b)  ((a)+(b)-1)/(b)

typedef unsigned long long ULL;
typedef signed long long SLL;


queue<int> _queue;


vector <vector <SLL>> memo2;
vector<SLL> memo1;
vector<SLL> c;
vector<SLL> K;
vector<SLL> dp;

SLL N, D, H ,A, B;



SLL C[15];

SLL solve(SLL target)
{
	SLL ans = 0;

	//cout << "_target=" << target << endl;

	// キャンディーの数の操作回数を求める
	// 1個づつしか操作できないので差分が操作回数になる
	for (int i = 0; i < N; i++)
	{
		//cout << c[i] << ": diff=" << abs(c[i] - target) << endl;
		ans += abs(C[i] - target);
	}
	//cout << "_ans=" << ans << endl;

	return ans;
}

int main()
{

	cin >> B;
	cin >> N;



	SLL minimum = LLONG_MAX;
	SLL total = 0;

	for (int i=0;i<N;i++)
	{
		cin >> C[i];
		if (C[i] < minimum) minimum = C[i];
		total += C[i];
	}
	
	// 答えは low と high の間にある
	SLL low  = minimum;          //各箱の中に最初から入っているキャンデーの数のなかで最も少ない数
	SLL high = (total + B) / N; //各箱に入れることが出来る最大数

	

	//箱の中のキャンディーの数
	SLL c1, c2;


	while (abs(high-low) > 2)
	{
		//cout << "low=" << low << ",high=" << high << endl;

		c1 = (high + 2 * low) / 3;	//箱の中のキャンディーの数の候補1
		c2 = (high * 2 + low) / 3;	//箱の中のキャンディーの数の候補2

		// それに必要な操作回数の比較
		if (solve(c1) < solve(c2))
			high = c2;
		else
			low = c1;
	}


	SLL ans = LLONG_MAX;

	//cout << "***low=" << low << ",high=" << high << endl;

	//最終的に絞り込んだ範囲の中での最小値が答えになる
	for (SLL i=low;i<=high;i++)
	{
		SLL tmp = solve(i);
		if (tmp < ans)
		{
			ans = tmp;
			//cout << "*ans=" << ans << endl;
		}
	}

	cout << ans << endl;


	//getchar();
	return 0; //end
}

0