結果

問題 No.545 ママの大事な二人の子供
ユーザー 0x19f0x19f
提出日時 2017-07-15 18:28:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 943 bytes
コンパイル時間 799 ms
コンパイル使用メモリ 77,884 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 21:50:06
合計ジャッジ時間 1,836 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

ll N;
vector<ll> A[2], B[2];

int main(void) {
  cin >> N;
  REP(i, 0, N) {
    ll a, b;
    cin >> a >> b;
    A[i % 2].push_back(a);
    B[i % 2].push_back(b);
  }

  if(N == 1) cout << min(abs(A[0][0]), B[0][0]) << endl;
  else {
    vector<ll> v[2];
    REP(g, 0, 2) {
      REP(i, 0, 1 << A[g].size()) {
        ll s = 0;
        REP(j, 0, A[g].size()) s += (i & (1 << j)) ? A[g][j] : -B[g][j];
        v[g].push_back(s);
      }
      sort(v[g].begin(), v[g].end());
    }
    ll ans = INF;
    REP(i, 0, v[0].size()) {
      int d = upper_bound(v[1].begin(), v[1].end(), -v[0][i]) - v[1].begin();
      ans = min(ans, abs(v[0][i] + v[1][d]));
      if(d - 1 >= 0) ans = min(ans, abs(v[0][i] + v[1][d - 1]));
    }
    cout << ans << endl;
  }
}
0