結果

問題 No.545 ママの大事な二人の子供
ユーザー takeya_okinotakeya_okino
提出日時 2017-07-15 09:41:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 1,665 bytes
コンパイル時間 3,184 ms
コンパイル使用メモリ 79,732 KB
実行使用メモリ 52,496 KB
最終ジャッジ日時 2024-04-16 21:37:10
合計ジャッジ時間 9,599 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
41,328 KB
testcase_01 AC 101 ms
41,024 KB
testcase_02 AC 113 ms
41,416 KB
testcase_03 AC 118 ms
41,340 KB
testcase_04 AC 106 ms
41,528 KB
testcase_05 AC 111 ms
41,492 KB
testcase_06 AC 115 ms
41,560 KB
testcase_07 AC 105 ms
41,488 KB
testcase_08 AC 118 ms
41,404 KB
testcase_09 AC 102 ms
41,588 KB
testcase_10 AC 109 ms
41,376 KB
testcase_11 AC 113 ms
41,300 KB
testcase_12 AC 120 ms
41,400 KB
testcase_13 AC 117 ms
41,400 KB
testcase_14 AC 122 ms
41,500 KB
testcase_15 AC 98 ms
41,648 KB
testcase_16 AC 133 ms
41,796 KB
testcase_17 AC 126 ms
41,868 KB
testcase_18 AC 117 ms
42,000 KB
testcase_19 AC 140 ms
43,080 KB
testcase_20 AC 166 ms
42,480 KB
testcase_21 AC 111 ms
41,384 KB
testcase_22 AC 252 ms
48,884 KB
testcase_23 AC 289 ms
52,256 KB
testcase_24 AC 234 ms
49,904 KB
testcase_25 AC 292 ms
52,496 KB
testcase_26 AC 297 ms
52,264 KB
testcase_27 AC 296 ms
52,112 KB
testcase_28 AC 320 ms
52,072 KB
testcase_29 AC 263 ms
52,156 KB
testcase_30 AC 274 ms
51,304 KB
testcase_31 AC 309 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    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();
    }
    int med = n / 2;
    ArrayList<Long> list1 = new ArrayList<Long>();
    for(int i = 0; i < (int)Math.pow(2, med + 1); i++) {
      long t = 0;
      for(int j = 0; j <= med; j++) {
        if((i & (1 << j)) != 0) {
          t += a[j];
        } else {
          t -= b[j];
        }
      }
      list1.add(t);
    }
    ArrayList<Long> list2 = new ArrayList<Long>();
    if(n - 2 - med >= 0) {
      for(int i = 0; i < (int)Math.pow(2, n - 1 - med); i++) {
        long t = 0;
        for(int j = 0; j < n - 1 - med; j++) {
          if((i & (1 << j)) != 0) {
            t += a[j + med + 1];
          } else {
            t -= b[j + med + 1];
          }
        }
        list2.add(t);
      }
    } else {
      list2.add((long)0);
    }
    Collections.sort(list2);
    long ans = (long)Math.pow(10, 15);
    for(int i = 0; i < list1.size(); i++) {
      long u = list1.get(i);
      int left = 0;
      int right = list2.size();
      int index = right - 1;
      while(left < right) {
        int med2 = (left + right) / 2;
        if(u + list2.get(med2) >= 0) {
          index = med2;
          right = med2;
        } else {
          left = med2 + 1;
        }
      }
      ans = Math.min(ans, Math.abs(u + list2.get(index)));
      if(index >= 1) ans = Math.min(ans, Math.abs(u + list2.get(index - 1)));
    }
    System.out.println(ans);
  }
}
0