結果
問題 | No.130 XOR Minimax |
ユーザー |
![]() |
提出日時 | 2021-11-10 11:59:33 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,179 ms / 5,000 ms |
コード長 | 1,819 bytes |
コンパイル時間 | 2,388 ms |
コンパイル使用メモリ | 78,292 KB |
実行使用メモリ | 88,908 KB |
最終ジャッジ日時 | 2024-11-20 21:53:15 |
合計ジャッジ時間 | 19,487 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); HashSet<Integer> list = new HashSet<>(); for (int i = 0; i < n; i++) { list.add(sc.nextInt()); } System.out.println(getMin(30, list)); } static int getMin(int idx, HashSet<Integer> list) { if (idx < 0) { return 0; } HashSet<Integer> odd = new HashSet<>(); HashSet<Integer> even = new HashSet<>(); for (int x : list) { if ((x & (1 << idx)) == 0) { even.add(x); } else { odd.add(x ^ (1 << idx)); } } if (even.size() == 0) { return getMin(idx - 1, odd); } else if (odd.size() == 0) { return getMin(idx - 1, even); } else { return (1 << idx) + Math.min(getMin(idx - 1, even), getMin(idx - 1, odd)); } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String nextLine() throws Exception { return br.readLine(); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }