Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Arrays;
- public class Integer_Distribution {
- private static long minXor;
- private static long maxXor;
- private static int getSetBitCount(int num) {
- int count = 0;
- while(num > 0) {
- if((num & 1) != 0) {
- count++;
- }
- num >>= 1;
- }
- return count;
- }
- private static void getPairXor(int array[], int N) {
- int SIZE = (int)Math.pow(2, N) - 1;
- long dpMin[] = new long[SIZE+1];
- long dpMax[] = new long[SIZE+1];
- dpMin[0] = 0;
- for(int i = 1;i <= SIZE;i++) {
- int setBitCount = getSetBitCount(i);
- dpMin[i] = Long.MAX_VALUE;
- dpMax[i] = Long.MIN_VALUE;
- if(setBitCount%2 == 0) {
- for(int j = 0;j < N;j++) {
- for(int k = (j+1);k < N;k++) {
- if(((i & (1<<j)) != 0) && ((i & (1 << k)) != 0)) {
- int num = i ^ (1 << j);
- num = num ^ (1 << k);
- long xor = array[j] ^ array[k];
- dpMin[i] = Math.min(dpMin[i], (xor + dpMin[num]));
- dpMax[i] = Math.max(dpMax[i], (xor + dpMax[num]));
- }
- }
- }
- }
- }
- minXor = dpMin[SIZE];
- maxXor = dpMax[SIZE];
- }
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
- StringBuilder sb = new StringBuilder();
- int N = Integer.parseInt(br.readLine());
- int array[] = Arrays.stream(br.readLine().split(" ")).mapToInt(c -> Integer.parseInt(c)).toArray();
- getPairXor(array, N);
- sb.append(minXor).append(" ").append(maxXor);
- bw.write(sb.toString());
- bw.close();
- }
- }
Add Comment
Please, Sign In to add comment