結果

問題 No.545 ママの大事な二人の子供
ユーザー 37zigen37zigen
提出日時 2016-07-21 21:13:07
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,482 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 77,432 KB
実行使用メモリ 43,640 KB
最終ジャッジ日時 2024-04-16 20:33:07
合計ジャッジ時間 8,243 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
41,120 KB
testcase_01 AC 122 ms
41,216 KB
testcase_02 AC 128 ms
41,528 KB
testcase_03 AC 127 ms
41,480 KB
testcase_04 AC 120 ms
41,360 KB
testcase_05 AC 129 ms
41,260 KB
testcase_06 AC 123 ms
41,388 KB
testcase_07 AC 128 ms
41,304 KB
testcase_08 AC 131 ms
41,132 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 125 ms
41,228 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 128 ms
41,296 KB
testcase_22 WA -
testcase_23 RE -
testcase_24 RE -
testcase_25 WA -
testcase_26 WA -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		solver();
	}

	static void solver() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] diff1 = new long[(int) pow(2, n / 2)];
		long[] diff2 = new long[(int) pow(2, n - n / 2)];
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
			b[i] = sc.nextInt();
		}
		for (int i = 0; i < n / 2; i++) {
			int d = (int) pow(2, i);
			for (int j = 0; j < d; j++) {
				diff1[j + d] = diff1[j] - b[i];
				diff1[j] = diff1[j] + a[i];
			}
		}
		int res = n / 2;
		for (int i = 0; i < n - n / 2; i++) {
			int d = (int) pow(2, i);
			for (int j = 0; j < d; j++) {
				diff2[j + d] = diff2[j] - b[i + res];
				diff2[j] = diff2[j] + a[i + res];
			}
		}
		Arrays.sort(diff1);
		Arrays.sort(diff2);

		long min = Long.MAX_VALUE;
		int j = diff2.length - 1;
		for (int i = 0; i < diff1.length; i++) {
			min = Math.min(min, Math.abs(diff1[i] + diff2[j]));
			while (j != 0 && min > Math.abs(diff1[i] + diff2[j - 1])) {
				j--;
				min = Math.abs(diff1[i] + diff2[j]);
			}
		}
		System.out.println(min);
	}

	static long pow(long a, long n) {
		long A = a;
		long ans = 1;
		while (n >= 1) {
			if (n % 2 == 0) {
				A = A * A;
				n /= 2;
			} else if (n % 2 == 1) {
				ans = ans * A;
				n--;
			}
		}
		return ans;
	}

	static void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
}
0