結果

問題 No.545 ママの大事な二人の子供
ユーザー te-shte-sh
提出日時 2017-07-14 23:09:15
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,783 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 113,660 KB
実行使用メモリ 5,096 KB
最終ジャッジ日時 2023-09-03 15:14:33
合計ジャッジ時間 2,307 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 24 ms
4,380 KB
testcase_23 AC 50 ms
5,096 KB
testcase_24 AC 12 ms
4,792 KB
testcase_25 AC 38 ms
4,380 KB
testcase_26 AC 26 ms
4,832 KB
testcase_27 AC 30 ms
4,376 KB
testcase_28 AC 50 ms
4,380 KB
testcase_29 AC 36 ms
4,380 KB
testcase_30 AC 32 ms
4,380 KB
testcase_31 AC 33 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap
import std.typecons;  // Tuple, Nullable, BigFlags
import std.math;      // math functions
import std.numeric;   // gcd, fft
import std.bigint;    // BigInt
import std.random;    // random
import std.bitmanip;  // BitArray
import core.bitop;    // bit operation
import std.regex;     // RegEx
import std.uni;       // unicode

void main()
{
  auto n = readln.chomp.to!size_t;
  auto a = new long[](n), b = new long[](n);
  foreach (i; 0..n) {
    auto rd = readln.split.to!(long[]);
    a[i] = rd[0];
    b[i] = rd[1];
  }

  if (n == 1) {
    writeln(min(a[0], b[0]));
    return;
  }

  auto n1 = n/2, n2 = n- n1;
  long[] s1, s2;

  foreach (i; 0..1 << n1) {
    auto sa = 0L, sb = 0L;
    foreach (j; 0..n1) {
      if (i.bitTest(j)) sa += a[j];
      else              sb += b[j];
    }
    s1 ~= sa - sb;
  }

  foreach (i; 0..1 << n2) {
    auto sa = 0L, sb = 0L;
    foreach (j; 0..n2) {
      if (i.bitTest(j)) sa += a[n1+j];
      else              sb += b[n1+j];
    }
    s2 ~= sa - sb;
  }

  s2.sort();
  auto s2s = s2.assumeSorted;

  auto r = long.max;
  foreach (s1i; s1) {
    auto t0 = s2s.equalRange(-s1i);
    if (!t0.empty) {
      writeln(0);
      return;
    }
    auto t1 = s2s.lowerBound(-s1i);
    if (!t1.empty) r = min(r, (s1i + t1.back).abs);
    auto t2 = s2s.upperBound(-s1i);
    if (!t2.empty) r = min(r, (s1i + t2.front).abs);
  }

  writeln(r);
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }
}
0