結果

問題 No.572 妖精の演奏
ユーザー asugen0402asugen0402
提出日時 2019-04-17 15:28:21
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 5,497 bytes
コンパイル時間 382 ms
コンパイル使用メモリ 35,960 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 07:26:35
合計ジャッジ時間 1,651 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 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 3 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 10 ms
4,348 KB
testcase_11 AC 23 ms
4,348 KB
testcase_12 AC 28 ms
4,348 KB
testcase_13 AC 38 ms
4,348 KB
testcase_14 AC 5 ms
4,348 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// 内部定数
#define D_SOUND_MAX		30												// 最大音数
#define D_IDX_MAX		40												// 最大指数

// 内部変数
static FILE *szpFpI;													// 入力
static long long slNCnt;												// 音符数
static long long sl2Sound[D_SOUND_MAX][D_SOUND_MAX];					// 音[元][先]
static int siSCnt;														// 音数
static long long sl3Score1[D_SOUND_MAX][D_SOUND_MAX][D_SOUND_MAX];		// スコア1[音数][開始音][終了音]
static long long sl3Score2[D_IDX_MAX][D_SOUND_MAX][D_SOUND_MAX];		// スコア2[音数*2のN乗][開始音][終了音]
static long long sl3Score3[D_IDX_MAX][D_SOUND_MAX][D_SOUND_MAX];		// スコア3[セット回数][開始音][終了音]

// 内部変数 - テスト用
#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;
}

// 実行メイン
long long
fMain(
)
{
	int i, j, k, l;
	char lc1Buf[1024];

	// 音符数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%lld", &slNCnt);
	if (slNCnt < 2) {
		return 0;
	}

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

	// 音 - 取得
	for (i = 0; i < siSCnt; i++) {
		for (j = 0; j < siSCnt; j++) {
			fscanf(szpFpI, "%lld", &sl2Sound[i][j]);
		}
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	}

	// スコア1 - セット
	long long llScore;
	for (i = 0; i < siSCnt; i++) {
		for (j = 0; j < siSCnt; j++) {
			sl3Score1[1][i][j] = sl2Sound[i][j];
		}
	}
	for (i = 2; i < siSCnt; i++) {
		for (j = 0; j < siSCnt; j++) {
			for (k = 0; k < siSCnt; k++) {
				for (l = 0; l < siSCnt; l++) {
					llScore = sl3Score1[i - 1][j][k] + sl2Sound[k][l];
					if (sl3Score1[i][j][l] < llScore) {
						sl3Score1[i][j][l] = llScore;
					}
				}
			}
		}
	}

	// スコア2 - セット
	for (i = 0; i < siSCnt; i++) {
		for (j = 0; j < siSCnt; j++) {
			sl3Score2[0][i][j] = sl3Score1[siSCnt - 1][i][j];
		}
	}
	int liNo2 = 0;
	long long llNCnt = siSCnt;
	while (llNCnt < slNCnt) {
		liNo2++;
		llNCnt *= 2;

		for (i = 0; i < siSCnt; i++) {
			for (j = 0; j < siSCnt; j++) {
				for (k = 0; k < siSCnt; k++) {
					for (l = 0; l < siSCnt; l++) {
						llScore = sl3Score2[liNo2 - 1][i][j] + sl2Sound[j][k] + sl3Score2[liNo2 - 1][k][l];
						if (sl3Score2[liNo2][i][l] < llScore) {
							sl3Score2[liNo2][i][l] = llScore;
						}
					}
				}
			}
		}
	}

	// スコア3 - セット - スコア2
	int liNo3 = 0;
	while (slNCnt >= siSCnt) {
		liNo2--;
		llNCnt /= 2;

		if (slNCnt >= llNCnt) {
			slNCnt -= llNCnt;
			for (i = 0; i < siSCnt; i++) {
				for (j = 0; j < siSCnt; j++) {
					if (liNo3 == 0) {
						sl3Score3[liNo3][i][j] = sl3Score2[liNo2][i][j];
					}
					else {
						for (k = 0; k < siSCnt; k++) {
							for (l = 0; l < siSCnt; l++) {
								llScore = sl3Score3[liNo3 - 1][i][j] + sl2Sound[j][k] + sl3Score2[liNo2][k][l];
								if (sl3Score3[liNo3][i][l] < llScore) {
									sl3Score3[liNo3][i][l] = llScore;
								}
							}
						}
					}
				}
			}
			liNo3++;
		}
	}

	// スコア3 - セット - スコア1
	if (slNCnt > 0) {
		for (i = 0; i < siSCnt; i++) {
			for (j = 0; j < siSCnt; j++) {
				if (liNo3 == 0) {
					sl3Score3[liNo3][i][j] = sl3Score1[slNCnt - 1][i][j];
				}
				else {
					for (k = 0; k < siSCnt; k++) {
						if (slNCnt == 1) {
							llScore = sl3Score3[liNo3 - 1][i][j] + sl2Sound[j][k];
							if (sl3Score3[liNo3][i][k] < llScore) {
								sl3Score3[liNo3][i][k] = llScore;
							}
						}
						else {
							for (l = 0; l < siSCnt; l++) {
								llScore = sl3Score3[liNo3 - 1][i][j] + sl2Sound[j][k] + sl3Score1[slNCnt - 1][k][l];
								if (sl3Score3[liNo3][i][l] < llScore) {
									sl3Score3[liNo3][i][l] = llScore;
								}
							}
						}
					}
				}
			}
		}
	}

	// 最大スコア - 取得
	long long llMax = 0;
	for (i = 0; i < siSCnt; i++) {
		for (j = 0; j < siSCnt; j++) {
			if (llMax < sl3Score3[liNo3][i][j]) {
				llMax = sl3Score3[liNo3][i][j];
			}
		}
	}

	return llMax;
}

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

	// データ - 初期化
	memset(sl3Score1, 0, sizeof(sl3Score1));					// スコア1
	memset(sl3Score2, 0, sizeof(sl3Score2));					// スコア2
	memset(sl3Score3, 0, sizeof(sl3Score3));					// スコア3

	// 入力 - セット
#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

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

	// 出力
	sprintf(lc1Buf, "%lld\n", llRet);
	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