結果

問題 No.545 ママの大事な二人の子供
ユーザー simansiman
提出日時 2022-04-11 08:15:44
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,338 bytes
コンパイル時間 5,298 ms
コンパイル使用メモリ 107,520 KB
実行使用メモリ 4,524 KB
最終ジャッジ日時 2023-08-21 15:03:14
合計ジャッジ時間 10,029 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 8 ms
4,376 KB
testcase_23 AC 16 ms
4,412 KB
testcase_24 AC 8 ms
4,380 KB
testcase_25 AC 17 ms
4,420 KB
testcase_26 AC 16 ms
4,420 KB
testcase_27 AC 16 ms
4,376 KB
testcase_28 AC 16 ms
4,376 KB
testcase_29 AC 16 ms
4,524 KB
testcase_30 AC 16 ms
4,432 KB
testcase_31 AC 16 ms
4,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;
int N;
ll A[40];
ll B[40];

vector<ll> S;
vector<ll> T;

void dfs(int i, int end, ll a_sum, ll b_sum) {
  if (i == end) {
    if (end < N) {
      S.push_back(a_sum - b_sum);
    } else {
      T.push_back(a_sum - b_sum);
    }
  } else {
    dfs(i + 1, end, a_sum + A[i], b_sum);
    dfs(i + 1, end, a_sum, b_sum + B[i]);
  }
}

int main() {
  cin >> N;

  for (int i = 0; i < N; ++i) {
    cin >> A[i] >> B[i];
  }

  if (N == 1) {
    dfs(0, N, 0, 0);
  } else {
    dfs(0, N / 2, 0, 0);
    dfs(N / 2, N, 0, 0);
  }

  sort(S.begin(), S.end());
  sort(T.begin(), T.end());
  ll ans = LLONG_MAX;

  if (N >= 2) {
    int L = T.size() - 1;
    for (ll s : S) {
      int idx = lower_bound(T.begin(), T.end(), -s) - T.begin();
      idx = min(idx, L);

      ll diff = abs(s + T[idx]);
      if (idx - 1 >= 0) {
        diff = min(diff, abs(s + T[idx - 1]));
      }
      if (idx + 1 <= L) {
        diff = min(diff, abs(s + T[idx + 1]));
      }

      ans = min(ans, diff);
    }
  } else {
    for (ll t : T) {
      ans = min(ans, abs(t));
    }
  }

  cout << ans << endl;

  return 0;
}
0