結果

問題 No.545 ママの大事な二人の子供
ユーザー fiordfiord
提出日時 2017-07-19 15:26:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,852 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 102,456 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 04:03:22
合計ジャッジ時間 2,170 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <utility>
#include <complex>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <tuple>
#include <bitset>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define rep(i,n)	for(int i=0;i<n;i++)
#define srep(i,a,n)	for(int i=a;i<n;i++)
#define REP(i,n)	for(int i=0;i<=n;i++)
#define SREP(i,a,n)	for(int i=a;i<=n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define RREP(i,n)	for(int i=n;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define mt	make_tuple
#define fst	first
#define scn second
#define bicnt(x)	__buildin__popcount(x)
#define debug(x)	cout<<"debug: "<<x<<endl

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-18;
const int dx[] = { 0,1,0,-1 };
const int dy[] = { 1,0,-1,0 };

vector<ll> s[2];
vector<ll> a, b;

void search(int i, int e, int t, ll val) {
	if (i == e)	s[t].push_back(val);
	else {
		search(i + 1, e, t, val + a[i]);
		search(i + 1, e, t, val - b[i]);
	}
}

int main() {
	int n;	cin >> n;
	a.resize(n);	b.resize(n);
	rep(i, n)	cin >> a[i] >> b[i];
	int n2 = n / 2;
	s[0].reserve(1 << (n2 + 1));
	s[1].reserve(1 << (n2 + 1));
	search(0, n2, 0, 0LL);
	search(n2, n, 1, 0LL);
	sort(all(s[0]));	sort(all(s[1]));
	int m = s[0].size();
	ll ret = inf;
	rep(i, m) {
		int j = lower_bound(s[1].begin(), s[1].end(), -s[0][i]) - s[1].begin();
		if (j > 0)	ret = min(ret, llabs(s[0][i] + s[1][j - 1]));
		if (j < s[1].size())	ret = min(ret, llabs(s[0][i] + s[1][j]));
	}
	cout << ret << endl;
	return 0;
}
0