結果

問題 No.130 XOR Minimax
ユーザー tentententen
提出日時 2021-03-08 09:32:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,217 ms / 5,000 ms
コード長 1,285 bytes
コンパイル時間 3,328 ms
コンパイル使用メモリ 78,888 KB
実行使用メモリ 69,140 KB
最終ジャッジ日時 2024-04-18 01:07:10
合計ジャッジ時間 22,637 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 685 ms
55,324 KB
testcase_01 AC 137 ms
41,376 KB
testcase_02 AC 122 ms
40,140 KB
testcase_03 AC 136 ms
41,100 KB
testcase_04 AC 657 ms
53,260 KB
testcase_05 AC 1,217 ms
69,140 KB
testcase_06 AC 979 ms
66,600 KB
testcase_07 AC 1,109 ms
65,944 KB
testcase_08 AC 1,201 ms
67,000 KB
testcase_09 AC 387 ms
49,432 KB
testcase_10 AC 494 ms
51,496 KB
testcase_11 AC 976 ms
60,364 KB
testcase_12 AC 299 ms
49,300 KB
testcase_13 AC 877 ms
58,112 KB
testcase_14 AC 1,137 ms
61,176 KB
testcase_15 AC 226 ms
44,808 KB
testcase_16 AC 882 ms
57,468 KB
testcase_17 AC 888 ms
57,044 KB
testcase_18 AC 978 ms
58,028 KB
testcase_19 AC 990 ms
58,572 KB
testcase_20 AC 701 ms
54,692 KB
testcase_21 AC 1,004 ms
59,716 KB
testcase_22 AC 459 ms
50,032 KB
testcase_23 AC 292 ms
47,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        System.out.println(getAnswer(30, list));
    }
    
    static int getAnswer(int idx, ArrayList<Integer> list) {
        int mask = (1 << idx);
        ArrayList<Integer> on = new ArrayList<>();
        ArrayList<Integer> off = new ArrayList<>();
        for (int x : list) {
            if ((mask & x) == 0) {
                off.add(x);
            } else {
                on.add(x);
            }
        }
        if (on.size() > 0 && off.size() > 0) {
            if (idx == 0) {
                return 1;
            } else {
                return mask + Math.min(getAnswer(idx - 1, on), getAnswer(idx - 1, off));
            }
        } else if (on.size() > 0) {
            if (idx == 0) {
                return 0;
            } else {
                return getAnswer(idx - 1, on);
            }
        } else {
            if (idx == 0) {
                return 0;
            } else {
                return getAnswer(idx - 1, off);
            }
        }
    }
}
0