結果

問題 No.545 ママの大事な二人の子供
ユーザー furafura
提出日時 2020-06-20 10:04:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 920 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 206,260 KB
実行使用メモリ 4,488 KB
最終ジャッジ日時 2023-09-16 16:59:45
合計ジャッジ時間 3,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 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,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 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,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 24 ms
4,376 KB
testcase_24 AC 12 ms
4,384 KB
testcase_25 AC 23 ms
4,376 KB
testcase_26 AC 24 ms
4,380 KB
testcase_27 AC 22 ms
4,472 KB
testcase_28 AC 24 ms
4,380 KB
testcase_29 AC 23 ms
4,380 KB
testcase_30 AC 22 ms
4,472 KB
testcase_31 AC 22 ms
4,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

const long long INF=1LL<<61;

int main(){
	int n; scanf("%d",&n);
	vector<lint> a(n),b(n);
	rep(i,n) scanf("%lld%lld",&a[i],&b[i]);

	lint asum=accumulate(a.begin(),a.end(),0LL);

	vector<lint> D1,D2;
	rep(S,1<<n/2){
		lint tmp=0;
		rep(i,n/2) if(S>>i&1) tmp+=a[i]+b[i];
		D1.emplace_back(tmp);
	}
	rep(S,1<<(n+1)/2){
		lint tmp=0;
		rep(i,(n+1)/2) if(S>>i&1) tmp+=a[n/2+i]+b[n/2+i];
		D2.emplace_back(tmp);
	}

	sort(D1.begin(),D1.end());
	sort(D2.begin(),D2.end());
	D1.erase(unique(D1.begin(),D1.end()),D1.end());
	D2.erase(unique(D2.begin(),D2.end()),D2.end());

	lint ans=INF;
	rep(i,D1.size()){
		int pos=lower_bound(D2.begin(),D2.end(),asum-D1[i])-D2.begin();
		for(int j=max(pos-2,0);j<min(pos+2,int(D2.size()));j++){
			ans=min(ans,abs(D1[i]+D2[j]-asum));
		}
	}
	printf("%lld\n",ans);

	return 0;
}
0