結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー 👑 ygussanyygussany
提出日時 2021-06-06 20:29:47
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 2,651 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 33,440 KB
実行使用メモリ 7,780 KB
最終ジャッジ日時 2023-08-15 09:58:44
合計ジャッジ時間 5,956 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,384 KB
testcase_02 AC 0 ms
4,384 KB
testcase_03 AC 0 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 84 ms
6,072 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 214 ms
7,340 KB
testcase_09 AC 38 ms
5,356 KB
testcase_10 AC 240 ms
7,488 KB
testcase_11 AC 227 ms
7,500 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 18 ms
4,608 KB
testcase_14 AC 119 ms
6,804 KB
testcase_15 AC 6 ms
4,384 KB
testcase_16 AC 67 ms
5,988 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 226 ms
7,512 KB
testcase_21 AC 0 ms
4,384 KB
testcase_22 AC 79 ms
7,084 KB
testcase_23 AC 253 ms
7,728 KB
testcase_24 AC 257 ms
7,608 KB
testcase_25 AC 255 ms
7,780 KB
testcase_26 AC 251 ms
7,776 KB
testcase_27 AC 253 ms
7,776 KB
testcase_28 AC 255 ms
7,772 KB
testcase_29 AC 251 ms
7,700 KB
testcase_30 AC 250 ms
7,664 KB
testcase_31 AC 250 ms
7,684 KB
testcase_32 AC 253 ms
7,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int inf = -(1 << 30);

void chmax(int* a, int b)
{
	if (*a < b) *a = b;
}

int main()
{
	int N;
	char S[501];
	scanf("%d", &N);
	scanf("%s", S);
	
	int i, j, k, l, m, dp[501][501][6], ans = -1, max[2];
	for (i = 0; i < N; i++) for (j = 0; j < N; j++) for (k = 0; k < 6; k++) dp[i][j][k] = inf;
	for (i = 0; i < N; i++) {
		dp[i][i][0] = 0;
		if (S[i] == 'p') dp[i][i][1] = 0;
		else if (S[i] == 'o') dp[i][i][2] = 0;
		else dp[i][i][3] = 0;
	}
	for (l = 1; l < N; l++) {
		for (i = 0; i < N - l; i++) {
			j = i + l;
			for (k = 0; k < 6; k++) {
				chmax(&(dp[i][j][k]), dp[i+1][j][k]);
				chmax(&(dp[i][j][k]), dp[i][j-1][k]);
			}
			if (S[i] == 'p') {
				chmax(&(dp[i][j][0]), dp[i+1][j][5] + 1);
				chmax(&(dp[i][j][1]), dp[i+1][j][0]);
				chmax(&(dp[i][j][4]), dp[i+1][j][2]);
			} else if (S[i] == 'o') {
				chmax(&(dp[i][j][2]), dp[i+1][j][0]);
				chmax(&(dp[i][j][5]), dp[i+1][j][3]);
			} else {
				chmax(&(dp[i][j][3]), dp[i+1][j][0]);
			}
			if (S[j] == 'p') {
				chmax(&(dp[i][j][1]), dp[i][j-1][0]);
			} else if (S[j] == 'o') {
				chmax(&(dp[i][j][2]), dp[i][j-1][0]);
				chmax(&(dp[i][j][4]), dp[i][j-1][1]);
			} else {
				chmax(&(dp[i][j][0]), dp[i][j-1][4] + 1);
				chmax(&(dp[i][j][3]), dp[i][j-1][0]);
				chmax(&(dp[i][j][5]), dp[i][j-1][2]);
			}
			
			for (m = i; m < j; m++) {
				chmax(&(dp[i][j][0]), dp[i][m][0] + dp[m+1][j][0]);
				chmax(&(dp[i][j][0]), dp[i][m][1] + dp[m+1][j][5] + 1);
				chmax(&(dp[i][j][0]), dp[i][m][4] + dp[m+1][j][3] + 1);
				chmax(&(dp[i][j][1]), dp[i][m][0] + dp[m+1][j][1]);
				chmax(&(dp[i][j][1]), dp[i][m][1] + dp[m+1][j][0]);
				chmax(&(dp[i][j][2]), dp[i][m][0] + dp[m+1][j][2]);
				chmax(&(dp[i][j][2]), dp[i][m][2] + dp[m+1][j][0]);
				chmax(&(dp[i][j][3]), dp[i][m][0] + dp[m+1][j][3]);
				chmax(&(dp[i][j][3]), dp[i][m][3] + dp[m+1][j][0]);
				chmax(&(dp[i][j][4]), dp[i][m][0] + dp[m+1][j][4]);
				chmax(&(dp[i][j][4]), dp[i][m][1] + dp[m+1][j][2]);
				chmax(&(dp[i][j][4]), dp[i][m][4] + dp[m+1][j][0]);
				chmax(&(dp[i][j][5]), dp[i][m][0] + dp[m+1][j][5]);
				chmax(&(dp[i][j][5]), dp[i][m][2] + dp[m+1][j][3]);
				chmax(&(dp[i][j][5]), dp[i][m][5] + dp[m+1][j][0]);
			}
			
			chmax(&(dp[i][j][1]), dp[i][j][4]);
			chmax(&(dp[i][j][2]), dp[i][j][4]);
			chmax(&(dp[i][j][2]), dp[i][j][5]);
			chmax(&(dp[i][j][3]), dp[i][j][5]);
			chmax(&(dp[i][j][0]), dp[i][j][1]);
			chmax(&(dp[i][j][0]), dp[i][j][2]);
			chmax(&(dp[i][j][0]), dp[i][j][3]);
		}
	}
	for (i = 0; i < N - 1; i++) if (dp[0][i][0] > 0 && dp[i+1][N-1][0] > 0) chmax(&ans, dp[0][i][0] + dp[i+1][N-1][0] - 2);
	printf("%d\n", ans);
	fflush(stdout);
	return 0;
}
0