結果

問題 No.545 ママの大事な二人の子供
ユーザー gzlcpgzlcp
提出日時 2017-07-14 22:58:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,287 bytes
コンパイル時間 2,080 ms
コンパイル使用メモリ 94,028 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-04-16 20:38:28
合計ジャッジ時間 2,543 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 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 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 29 ms
6,808 KB
testcase_23 AC 61 ms
10,368 KB
testcase_24 AC 26 ms
6,784 KB
testcase_25 AC 64 ms
10,368 KB
testcase_26 AC 59 ms
10,368 KB
testcase_27 AC 58 ms
10,368 KB
testcase_28 AC 58 ms
10,368 KB
testcase_29 AC 60 ms
10,368 KB
testcase_30 AC 65 ms
10,496 KB
testcase_31 AC 64 ms
10,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define REP(i, n) for(long long i = 0; i < n; ++i)
#define ALL(a) a.begin(), a.end()
#define INF (long long)1000000000
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

int main() {
	int n;
	cin>>n;
	vector<P> a(n);
	REP(i, n) cin>>a[i].first>>a[i].second;
	int half = n/2;
	set<ll> aover1;
	set<ll> bover1;
	set<ll> aover2;
	set<ll> bover2;
	for(ll bit = 0; bit < (1<<half); ++bit) {
		ll asum = 0, bsum = 0;
		REP(i, half) {
			if((bit>>i)&1) asum += a[i].first;
			else bsum += a[i].second;
		}
		if(asum >= bsum) aover1.insert(asum - bsum);
		if(asum <= bsum) bover1.insert(bsum - asum);
	}
	for(ll bit = 0; bit < (1<<(n - half)); ++bit) {
		ll asum = 0, bsum = 0;
		REP(i, n - half) {
			if((bit>>i)&1) asum += a[half + i].first;
			else bsum += a[half + i].second;
		}
		if(asum >= bsum) aover2.insert(asum - bsum);
		if(asum <= bsum) bover2.insert(bsum - asum);
	}
	ll res = INF * INF;
	if(aover1.size() > 0 && aover2.size() > 0) res = min(res, *(aover1.begin()) + *(aover2.begin()));
	if(bover1.size() > 0 && bover2.size() > 0) res = min(res, *(bover1.begin()) + *(bover2.begin()));
	vector<ll> ao1(aover1.size());
	vector<ll> ao2(aover2.size());
	vector<ll> bo1(bover1.size());
	vector<ll> bo2(bover2.size());
	auto ite = aover1.begin();
	int roc = 0;
	while(ite != aover1.end()) {
		ao1[roc] = *ite;
		++ite;
		++roc;
	}
	ite = aover2.begin();
	roc = 0;
	while(ite != aover2.end()) {
		ao2[roc] = *ite;
		++ite;
		++roc;
	}
	ite = bover1.begin();
	roc = 0;
	while(ite != bover1.end()) {
		bo1[roc] = *ite;
		++ite;
		++roc;
	}
	ite = bover2.begin();
	roc = 0;
	while(ite != bover2.end()) {
		bo2[roc] = *ite;
		++ite;
		++roc;
	}
	roc = 0;
	while(roc < ao1.size()) {
		auto ite1 = lower_bound(ALL(bo2), ao1[roc]);
		if(ite1 != bo2.end()) {
			res = min(res, abs(ao1[roc] - *ite1));
		}
		if(ite1 != bo2.begin()) {
			res = min(res, abs(ao1[roc] - *(--ite1)));
		}
		++roc;
	}
	roc = 0;
	while(roc < bo1.size()) {
		auto ite2 = lower_bound(ALL(ao2), bo1[roc]);
		if(ite2 != ao2.end()) {
			res = min(res, abs(bo1[roc] - *ite2));
		}
		if(ite2 != ao2.begin()) {
			res = min(res, abs(bo1[roc] - *(--ite2)));
		}
		++roc;
	}
	cout<<res<<endl;
}
0