結果

問題 No.37 遊園地のアトラクション
ユーザー asugen0402asugen0402
提出日時 2019-01-24 15:50:13
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 4,325 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 31,056 KB
実行使用メモリ 7,780 KB
最終ジャッジ日時 2023-10-14 09:38:52
合計ジャッジ時間 1,925 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 4 ms
7,776 KB
testcase_02 AC 6 ms
7,644 KB
testcase_03 WA -
testcase_04 AC 7 ms
7,684 KB
testcase_05 AC 9 ms
7,768 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 7 ms
7,680 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 4 ms
7,680 KB
testcase_24 AC 3 ms
7,644 KB
testcase_25 AC 4 ms
7,704 KB
testcase_26 AC 4 ms
7,740 KB
testcase_27 AC 7 ms
7,776 KB
testcase_28 AC 4 ms
7,768 KB
testcase_29 AC 5 ms
7,772 KB
testcase_30 AC 4 ms
7,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// 内部定数
#define D_ATR_MAX		15 * 10									// 最大アトラクション数
#define D_TIME_MAX		10000									// 最大滞在時間

// 内部構造体 - アトラクション情報
typedef struct Atr {
	int miTime;													// 行列時間
	int miVal;													// 満足度
} Atr;

// 内部変数
static FILE *szpFpI;											// 入力
static int siTime;												// 滞在時間
static Atr sz1Atr[D_ATR_MAX];									// アトラクション
static int siACnt;												// アトラクション数
static int si2Val[D_ATR_MAX][D_TIME_MAX + 5];					// 満足度[アトラクション][滞在時間]

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

// ソート関数 - (満足度 / 行列時間) の降順
int
fSortFnc(
	const void *pzpVal1			// <I> 値1
	, const void *pzpVal2		// <I> 値2
)
{
	Atr *lzpVal1 = (Atr *)pzpVal1;
	Atr *lzpVal2 = (Atr *)pzpVal2;

	// 満足度 / 行列時間
	double ldRate1 = (double)lzpVal1->miVal / (double)lzpVal1->miTime;
	double ldRate2 = (double)lzpVal2->miVal / (double)lzpVal2->miTime;

	// (満足度 / 行列時間) の降順
	if (ldRate1 > ldRate2) {
		return(-1);
	}
	else if (ldRate1 < ldRate2) {
		return(1);
	}

	return 0;
}

// 満足度 - セット
int
fSetVal(
	int piTime					// <I> 滞在時間
	, int piVal					// <I> 満足度
	, int piANo					// <I> アトラクション
)
{
	// 終了チェック
	if (piANo >= siACnt) {
		return 0;
	}

	// 滞在時間 - チェック
	if (piTime > siTime) {
		return 0;
	}

	// 満足度 - チェック・更新
	if (piTime > 0) {
		if (si2Val[piANo][piTime] >= piVal) {
			return 0;
		}
		si2Val[piANo][piTime] = piVal;
	}

	// アトラクション - 使用
	fSetVal(piTime + sz1Atr[piANo].miTime, piVal + sz1Atr[piANo].miVal, piANo + 1);

	// アトラクション - 未使用
	fSetVal(piTime, piVal, piANo + 1);

	return 0;
}

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

	// データ - 初期化
	memset(si2Val, 0, sizeof(si2Val));							// 満足度[アトラクション][滞在時間]

	// 入力 - セット
#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", &siTime);

	// アトラクション数 - 取得
	int liACnt;
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &liACnt);

	// 行列時間 - 取得
	for (i = 0; i < liACnt; i++) {
		fscanf(szpFpI, "%d", &sz1Atr[i].miTime);
	}
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);

	// 満足度 - 取得
	for (i = 0; i < liACnt; i++) {
		fscanf(szpFpI, "%d", &sz1Atr[i].miVal);
	}
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);

	// アトラクション数
	siACnt = liACnt;

	// 2回目以降のアトラクション - 追加
	for (i = 0; i < liACnt; i++) {
		int liVal = sz1Atr[i].miVal;
		while (liVal > 1) {
			liVal /= 2;
			sz1Atr[siACnt].miTime = sz1Atr[i].miTime;
			sz1Atr[siACnt].miVal = liVal;
			siACnt++;
		}
	}

	// アトラクション - ソート
	qsort(sz1Atr, siACnt, sizeof(Atr), fSortFnc);

	// 満足度 - セット
	fSetVal(0, 0, 0);

	// 最大満足度 - 取得
	int liMax = INT_MIN;
	for (i = 0; i <= siTime; i++) {
		if (liMax < si2Val[siACnt - 1][i]) {
			liMax = si2Val[siACnt - 1][i];
		}
	}

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

	// 結果 - 表示
#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