結果

問題 No.334 門松ゲーム
ユーザー bal4u
提出日時 2019-09-03 19:32:50
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,116 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 29,824 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-24 12:52:53
合計ジャッジ時間 1,762 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: 334 門松ゲーム
// 2019.9.3 bal4u

#include <stdio.h>
#include <string.h>

int k[15], N;
char f[15];

int check(int a, int b, int c) { return a != c && ((a < b && b > c) || (a > b && b < c)); }

int rec(int n, int isD) {
	int i, a, b, c, t;
	
	if (n < 3) return !isD;
	for (a = 0; a < N; a++) if (f[a]) {
		f[a] = 0;
		for (b = a+1; b < N; b++) if (f[b]) {
			f[b] = 0;
			for (c = b+1; c < N; c++) if (f[c]) {
				if (check(k[a], k[b], k[c])) {
					if (n == 3) { f[b] = f[a] = 1; return isD; }
					f[c] = 0, t = rec(n-3, !isD), f[c] = 1;
					if (t == isD) { f[a] = f[b] = 1; return isD; }
				}
			}
			f[b] = 1;
		}
		f[a] = 1;
	}
	return !isD;
}
							
int main()
{
	int i, a, b, c;
	
	scanf("%d", &N);
	for (i = 0; i < N; i++) scanf("%d", k+i);
	memset(f, 1, sizeof(f));
	for (a = 0; a < N; a++) {
		f[a] = 0; for (b = a+1; b < N; b++) {
			f[b] = 0; for (c = b+1; c < N; c++) if (check(k[a], k[b], k[c])) {
				f[c] = 0;
				if (N == 3 || rec(N-3, 0) == 1) goto end;
				f[c] = 1;
			}
			f[b] = 1;
		}
		f[a] = 1;
	}
	puts("-1");
	return 0;
end:printf("%d %d %d\n", a, b, c);
	return 0;
}
0