結果
問題 | No.334 門松ゲーム |
ユーザー | ぴろず |
提出日時 | 2016-01-15 22:52:27 |
言語 | Java19 (openjdk 21) |
結果 |
AC
|
実行時間 | 229 ms / 2,000 ms |
コード長 | 1,232 bytes |
コンパイル時間 | 2,340 ms |
コンパイル使用メモリ | 79,196 KB |
実行使用メモリ | 59,072 KB |
最終ジャッジ日時 | 2023-10-19 23:27:22 |
合計ジャッジ時間 | 6,317 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 168 ms
58,612 KB |
testcase_01 | AC | 132 ms
57,704 KB |
testcase_02 | AC | 196 ms
58,300 KB |
testcase_03 | AC | 169 ms
58,612 KB |
testcase_04 | AC | 169 ms
58,612 KB |
testcase_05 | AC | 173 ms
58,604 KB |
testcase_06 | AC | 188 ms
58,684 KB |
testcase_07 | AC | 170 ms
57,804 KB |
testcase_08 | AC | 193 ms
58,540 KB |
testcase_09 | AC | 178 ms
57,788 KB |
testcase_10 | AC | 187 ms
58,096 KB |
testcase_11 | AC | 218 ms
58,880 KB |
testcase_12 | AC | 229 ms
58,884 KB |
testcase_13 | AC | 222 ms
58,984 KB |
testcase_14 | AC | 178 ms
57,948 KB |
testcase_15 | AC | 226 ms
59,072 KB |
ソースコード
package no334; import java.util.Scanner; public class Main { static int n; static int[] k; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); k = new int[n]; for(int i=0;i<n;i++) { k[i] = sc.nextInt(); } int ans = dfs((1<<n)-1); if (ans == Integer.MAX_VALUE) { System.out.println(-1); }else{ System.out.println(ans / 10000 + " " + (ans / 100 % 100) + " " + ans % 100); } } static int[] memo = new int[1<<12]; //先手が勝てるか public static int dfs(int m) { if (memo[m] != 0) { return memo[m]; } int[] use = new int[3]; int ret = Integer.MAX_VALUE; for(int i=0;i<1<<n;i++) { if ((~m & i) != 0 || Integer.bitCount(i) != 3) { continue; } int ind = 0; for(int j=0;j<n;j++) { if ((i >> j & 1) == 1) { use[ind++] = j; } } if (isKadomatsuSequence(k[use[0]], k[use[1]], k[use[2]])) { if (dfs(m & ~i) == Integer.MAX_VALUE) { ret = Math.min(ret, use[0] * 10000 + use[1] * 100 + use[2]); } } } return memo[m] = ret; } public static boolean isKadomatsuSequence(long a,long b,long c) { if (a == b || b == c) { return false; } return b < a && b < c || b > a && b > c; } }