結果

問題 No.634 硬貨の枚数1
ユーザー asugen0402asugen0402
提出日時 2019-04-18 17:31:47
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 3,856 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 32,248 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2023-10-22 08:21:11
合計ジャッジ時間 12,121 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 3 ms
6,688 KB
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 2 ms
4,360 KB
testcase_46 AC 3 ms
6,688 KB
testcase_47 RE -
testcase_48 AC 1 ms
4,348 KB
testcase_49 AC 3 ms
6,688 KB
testcase_50 RE -
testcase_51 AC 3 ms
6,688 KB
testcase_52 AC 1 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 1 ms
4,348 KB
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
testcase_69 RE -
testcase_70 RE -
testcase_71 RE -
testcase_72 RE -
testcase_73 RE -
testcase_74 RE -
testcase_75 RE -
testcase_76 WA -
testcase_77 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// 内部定数
#define D_VAL_MAX		1000005									// 最大値

// 内部変数
static FILE *szpFpI;											// 入力
static int siPay;												// 支払い額
static int si1Coin[D_VAL_MAX];									// コイン
static int siCCnt;												// コイン数
static int si1UCnt[D_VAL_MAX];									// 使用コイン数[支払い額]

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

// 出力
int
fOut(
	char *pcpLine				// <I> 1行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// 検索+前後
// 戻り値:[>=0]配列番号 [-1]なし
int
fBSrhPN(
	int piVal					// <I> 値
	, int *pipArray				// <I> 配列
	, int piACnt				// <I> 配列数
	, int *pipPNo				// <O> [>=0]1つ前の値の配列番号 [-1]なし
	, int *pipNNo				// <O> [>=0]1つ後の値の配列番号 [siACnt]なし
)
{
	// 初期範囲
	int liSNo = 0;
	int liENo = piACnt - 1;

	// 検索
	while (1) {

		// 中間位置
		int liMNo = (liSNo + liENo) / 2;

		// 一致チェック
		if (piVal == pipArray[liMNo]) {
			*pipPNo = liMNo - 1;
			*pipNNo = liMNo + 1;
			return liMNo;
		}

		// 範囲を絞る
		if (piVal < pipArray[liMNo]) {				// 左側へ
			if (liSNo < liMNo) {						// 範囲あり
				liENo = liMNo - 1;
			}
			else {										// 範囲なし
				*pipPNo = liMNo - 1;
				*pipNNo = liMNo;
				return -1;
			}
		}
		else {										// 右側へ
			if (liENo > liMNo) {						// 範囲あり
				liSNo = liMNo + 1;
			}
			else {										// 範囲なし
				*pipPNo = liMNo;
				*pipNNo = liMNo + 1;
				return -1;
			}
		}
	}

	return -1;
}

// 実行メイン
int
fMain(
)
{
	int i;
	char lc1Buf[1024];

	// 支払い額 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siPay);

	// コイン - セット
	for (siCCnt = 0; ; siCCnt++) {
		si1Coin[siCCnt] = (siCCnt + 1) * (siCCnt + 2) / 2;
		if (si1Coin[siCCnt] > siPay) {
			break;
		}
	}

	// 使用コイン数 - 初期化
	for (i = 0; i <= siPay; i++) {
		si1UCnt[i] = i;
	}

	// 使用コイン数 - セット
	for (i = siCCnt - 1; i >= 0; i--) {
		int liCoin = si1Coin[i];
		int liVal = liCoin;
		int liCnt = 1;
		while (1) {
			if (si1UCnt[liVal] <= liCnt) {
				break;
			}
			si1UCnt[liVal] = liCnt;

			// 残額
			int liVal2 = siPay - liVal;
			if (liVal2 == 0) {
				break;
			}

			// 次のコイン
			if (liVal2 < liCoin) {
				int liPNo, liNNo;
				int liNo = fBSrhPN(liVal2, si1Coin, siCCnt, &liPNo, &liNNo);
				if (liNo < 0) {
					liNo = liPNo;
				}
				liCoin = si1Coin[liNo];
			}

			// 次の額
			liVal += liCoin;
			liCnt++;
		}
	}

	return si1UCnt[siPay];
}

// 1回実行
int
fOne(
)
{
	int liRet;
	char lc1Buf[1024];

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

	// 実行メイン
	liRet = fMain();

	// 出力
	sprintf(lc1Buf, "%d\n", liRet);
	fOut(lc1Buf);

	// 残データ有無
#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", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

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

	return 0;
}

0