結果

問題 No.545 ママの大事な二人の子供
ユーザー Jumbo_kprJumbo_kpr
提出日時 2019-08-02 14:34:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,188 bytes
コンパイル時間 952 ms
コンパイル使用メモリ 103,540 KB
実行使用メモリ 6,260 KB
最終ジャッジ日時 2023-09-18 18:14:30
合計ジャッジ時間 2,308 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#pragma GCC optimize ("-O3","unroll-loops")
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>


#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<'\n';
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()
#define double long double

using namespace std;

int MOD = (ll)1000000000 + 7;
const ll INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int>P;
const int MAX = 200020;

int A[40], B[40];
int N;
vector<int>v1, v2,ans;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	REP(i, N) {
		cin >> A[i] >> B[i];
	}
	int h = N / 2;
	if (h == 0)goto through;
	REP(bit, 1LL << h) {
		int res = 0;
		REP(i, h) {
			if (bit & (1LL < i)) {
				res += A[i];
			}
			else res -= B[i];
		}
		v1.pb(res);
		//print(res);
		//print("here");
		ans.pb(abs(res));
	}
through:;
	REP(bit, 1LL << (N - h)) {
		int res = 0;
		REP(i, N - h) {
			if (bit & (1 << i)) {
				res += A[h + i];
			}
			else res -= B[h + i];
		}
		//print(res);
		v2.pb(res);
		ans.pb(abs(res));
	}
	v2.pb(-INF); v2.pb(INF);
	VSORT(v2);
	VSORT(ans);
	if (N == 1) {
		print(ans[0]); return 0;
	}
	VSORT(v1);
	ll ret = INF;
	REP(i, v1.size()) {
		auto itr = lb(v2, -v1[i]);
		int res1 = abs(v1[i] + *itr);
		itr--;
		int res2 = abs(v1[i] + *itr);

		itr++, itr++;
		int res3 = abs(v1[i] + *itr);
		ret = min({ ret,res1,res2,res3 });
	}
	print(ret);
}
0