結果

問題 No.198 キャンディー・ボックス2
ユーザー asugen0402asugen0402
提出日時 2019-01-17 17:16:22
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 3,607 bytes
コンパイル時間 1,178 ms
コンパイル使用メモリ 30,372 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 02:02:20
合計ジャッジ時間 1,336 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_BOX_MAX		10										// 最大箱数

// 内部変数
static FILE *szpFpI;											// 入力
static int siHCnt;												// 所持数
static int si1Box[D_BOX_MAX];									// 箱内数
static int siBCnt;												// 箱数
static long long slWCnt;										// 作業数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
#endif

// ソート関数 - int昇順
int
fSortFnc(
	const void *pzpVal1			// <I> 値1
	, const void *pzpVal2		// <I> 値2
)
{
	int *lipVal1 = (int *)pzpVal1;
	int *lipVal2 = (int *)pzpVal2;

	// int昇順
	if (*lipVal1 > *lipVal2) {
		return(1);
	}
	else if (*lipVal1 < *lipVal2) {
		return(-1);
	}

	return 0;
}

// 作業数 - 取得・更新
long long
fGetWCnt(
	int piCnt					// <I> 個数
)
{
	int i;

	// 必要数・作業数
	long long llNCnt = 0;
	long long llWCnt = 0;
	for (i = 0; i < siBCnt; i++) {
		llWCnt += (long long)abs(piCnt - si1Box[i]);
		llNCnt += (long long)(piCnt - si1Box[i]);
	}

	// 所持数
	if ((long long)siHCnt < llNCnt) {
		return LLONG_MAX;
	}

	// 作業数 - 更新
	if (slWCnt > llWCnt) {
		slWCnt = llWCnt;
	}

	return llWCnt;
}

// 作業数 - 更新
int
fUpWCnt(
	int piMin					// <I> 範囲 - 最小個数
	, int piMax					// <I> 範囲 - 最大個数
)
{
	// 範囲チェック
	if (piMin > piMax) {
		return 0;
	}

	// 作業数 - 最小個数
	long long llMin = fGetWCnt(piMin);

	// 作業数 - 最大個数
	long long llMax = fGetWCnt(piMax);

	// 加算値
	int liAdd = (piMax - piMin) / 3;

	// 作業数 - 範囲1
	int liVal1 = piMin + liAdd;
	long long llVal1 = fGetWCnt(liVal1);

	// 作業数 - 範囲2
	int liVal2 = piMax - liAdd;
	long long llVal2 = fGetWCnt(liVal2);

	// 範囲を絞る
	if (llVal1 <= llVal2) {
		fUpWCnt(piMin + 1, liVal2 - 1);
	}
	else {
		fUpWCnt(liVal1 + 1, piMax - 1);
	}

	return 0;
}

// 実行メイン
int
fMain(
	int piTNo					// <I> テスト番号 1~
)
{
	int i;
	char lc1Buf[1024], lc1Out[1024];

	// データ - 初期化
	slWCnt = LLONG_MAX;											// 作業数

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 所持数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siHCnt);

	// 箱数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siBCnt);

	// 箱内数 - 取得
	for (i = 0; i < siBCnt; i++) {
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
		sscanf(lc1Buf, "%d", &si1Box[i]);
	}

	// 箱内数 - ソート
	qsort(si1Box, siBCnt, sizeof(int), fSortFnc);

	// 作業数 - 更新
	fUpWCnt(si1Box[0], si1Box[siBCnt - 1]);

	// 結果 - セット
	sprintf(lc1Out, "%lld\n", slWCnt);

	// 結果 - 表示
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, lc1Out)) {
		siRes = -1;
	}
#else
	printf("%s", lc1Out);
#endif

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", piTNo);
	}
	else {
		printf("NG %d\n", piTNo);
	}
#endif

	return 0;
}

int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		fMain(i);
	}
#else
	fMain(0);
#endif

	return 0;
}

0