結果

問題 No.334 門松ゲーム
ユーザー crazybbb3crazybbb3
提出日時 2016-11-28 18:08:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 2,836 bytes
コンパイル時間 2,543 ms
コンパイル使用メモリ 79,072 KB
実行使用メモリ 38,620 KB
最終ジャッジ日時 2024-05-05 14:38:30
合計ジャッジ時間 4,989 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
38,116 KB
testcase_01 AC 48 ms
36,968 KB
testcase_02 AC 48 ms
36,736 KB
testcase_03 AC 76 ms
38,164 KB
testcase_04 AC 78 ms
38,292 KB
testcase_05 AC 74 ms
38,272 KB
testcase_06 AC 82 ms
38,540 KB
testcase_07 AC 46 ms
36,908 KB
testcase_08 AC 75 ms
38,404 KB
testcase_09 AC 47 ms
37,072 KB
testcase_10 AC 51 ms
37,116 KB
testcase_11 AC 80 ms
38,620 KB
testcase_12 AC 81 ms
38,136 KB
testcase_13 AC 83 ms
38,428 KB
testcase_14 AC 49 ms
36,968 KB
testcase_15 AC 77 ms
38,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    void solve() throws IOException {
        n = ni();
        k = nia(n);

        memo = new int[1 << n];
        Arrays.fill(memo, -1);

        int mask = rec((1 << n) - 1);
        if (mask == 0) {
            out.println(-1);
            return;
        }

        String ans = "";

        for (int i = 0; i < n; i++) {
            if ((mask >> i & 1) == 1) {
                if (!"".equals(ans)) ans += " ";
                ans += i;
            }
        }

        out.println(ans);
    }

    int n;
    int[] k;

    int[] memo;

    int rec(int x) {
        if (memo[x] >= 0) return memo[x];

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int l = j + 1; l < n; l++) {
                    if ((x >> i & 1) == 0) continue;
                    if ((x >> j & 1) == 0) continue;
                    if ((x >> l & 1) == 0) continue;

                    if (k[i] <= k[j] && k[j] <= k[l]) continue;
                    if (k[i] >= k[j] && k[j] >= k[l]) continue;

                    int mask = 1 << i | 1 << j | 1 << l;
                    if (rec(x ^ mask) == 0) {
                        return memo[x] = mask;
                    }
                }
            }
        }

        return memo[x] = 0;
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }

    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}
0