結果

問題 No.545 ママの大事な二人の子供
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-26 09:06:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,222 bytes
コンパイル時間 1,530 ms
コンパイル使用メモリ 133,636 KB
実行使用メモリ 4,492 KB
最終ジャッジ日時 2023-10-26 09:06:05
合計ジャッジ時間 3,733 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
#include<functional>
#include<utility>
using namespace std;
int N;
long long A[32],B[32];
void solve()
{
	cin >> N;
	for(int i = 0;i < N;i++) cin >> A[i] >> B[i];
	vector<long long> d,u;
	for(int i = 0;i < 1 << N/2;i++)
	{
		long long S = 0;
		for(int j = 0;j < N/2;j++)
		{
			if(i >> j & 1) S += A[j];
			else S -= B[j];
		}
		d.push_back(S);
	}

	for(int i = 0;i < 1 << (N-N/2);i++)
	{
		long long S = 0;
		for(int j = 0;j < N-N/2;j++)
		{
			if(i >> j & 1) S += A[j+N/2];
			else S -= B[j+N/2];
		}
		u.push_back(S);
	}
	sort(u.begin(),u.end());
	long long ans = (long long)9e18;
	for(int i = 0;i < (int)d.size();i++)
	{
		auto it = lower_bound(u.begin(),u.end(),-d[i]);
		if(it != u.end()) ans = min(ans,abs(*it+d[i]));
		if(it != u.begin()) ans = min(ans,abs(*(it-1)+d[i]));
	}
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
}
0