結果

問題 No.545 ママの大事な二人の子供
ユーザー 37zigen37zigen
提出日時 2016-07-21 21:31:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 1,529 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 77,564 KB
実行使用メモリ 44,084 KB
最終ジャッジ日時 2024-04-16 20:34:05
合計ジャッジ時間 8,790 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
40,196 KB
testcase_01 AC 129 ms
41,440 KB
testcase_02 AC 117 ms
40,232 KB
testcase_03 AC 128 ms
41,376 KB
testcase_04 AC 126 ms
41,380 KB
testcase_05 AC 115 ms
40,308 KB
testcase_06 AC 113 ms
40,424 KB
testcase_07 AC 115 ms
39,972 KB
testcase_08 AC 129 ms
41,420 KB
testcase_09 AC 119 ms
40,140 KB
testcase_10 AC 129 ms
41,444 KB
testcase_11 AC 126 ms
41,328 KB
testcase_12 AC 117 ms
40,356 KB
testcase_13 AC 120 ms
40,124 KB
testcase_14 AC 134 ms
41,632 KB
testcase_15 AC 132 ms
41,532 KB
testcase_16 AC 135 ms
41,256 KB
testcase_17 AC 135 ms
41,640 KB
testcase_18 AC 140 ms
41,604 KB
testcase_19 AC 167 ms
41,780 KB
testcase_20 AC 172 ms
41,960 KB
testcase_21 AC 137 ms
41,288 KB
testcase_22 AC 226 ms
43,076 KB
testcase_23 AC 284 ms
43,684 KB
testcase_24 AC 231 ms
43,188 KB
testcase_25 AC 278 ms
44,024 KB
testcase_26 AC 284 ms
43,904 KB
testcase_27 AC 286 ms
44,084 KB
testcase_28 AC 289 ms
43,728 KB
testcase_29 AC 290 ms
43,692 KB
testcase_30 AC 285 ms
43,996 KB
testcase_31 AC 268 ms
43,836 KB
権限があれば一括ダウンロードができます

ソースコード

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)];
		long[] a = new long[n];
		long[] b = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextLong();
			b[i] = sc.nextLong();
		}
		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 && Math.abs(diff1[i] + diff2[j]) >= Math.abs(diff1[i] + diff2[j - 1])) {
				j--;
				min = Math.min(Math.abs(diff1[i] + diff2[j]), min);
			}
		}
		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