結果
問題 |
No.5001 排他的論理和でランニング
|
ユーザー |
![]() |
提出日時 | 2018-03-16 23:49:11 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,380 ms / 1,500 ms |
コード長 | 1,782 bytes |
コンパイル時間 | 2,347 ms |
実行使用メモリ | 56,312 KB |
スコア | 52,421,139 |
最終ジャッジ日時 | 2020-03-12 19:40:25 |
ジャッジサーバーID (参考情報) |
judge9 / |
純コード判定しない問題か言語 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 50 |
ソースコード
import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); int[] array = new int[N]; for(int i = 0; i < N; i++){ array[i] = sc.nextInt(); } Arrays.sort(array); boolean[] used = new boolean[N]; int count = 0; long xor = 0; for(int bit = 31; bit >= 0 && count < M; bit--){ if(((1 << bit) & xor) != 0){ continue; } int max_index = -1; long max_value = -1; for(int i = 0; i < N; i++){ if(used[i]){ continue; } if(((1 << bit) & array[i]) == 0){ continue; } if(max_value < array[i]){ max_index = i; max_value = array[i]; } } if(max_index >= 0){ used[max_index] = true; xor ^= max_value; count++; } } //System.out.println(xor + " " + count + " : " + Arrays.toString(used)); for(int i = 0; i < N && count < M; i++){ if(used[i]){ continue; } used[i] = true; xor ^= array[i]; count++; } //System.out.println(xor + " " + count + " : " + Arrays.toString(used)); for(int x = 0; x < 9000000; x++){ final int fst = (int)(Math.random() * N); final int snd = (int)(Math.random() * N); if(used[fst] && used[snd]){ continue; } if(!used[fst] && !used[snd]){ continue; } final long next_xor = xor ^ array[fst] ^ array[snd]; if(next_xor > xor){ xor = next_xor; used[fst] = !used[fst]; used[snd] = !used[snd]; } } boolean first = true; for(int i = 0; i < N; i++){ if(!used[i]){ continue; } System.out.print((first ? "" : " ") + (array[i])); first = false; } System.out.println(); } }