MayTheBestPetWin

dp #数学 #Topcoder

\(s_1=\sum a_i,s2=\sum b_i\),选中的集合为 \(s\),设 \(p=[x\in s](a_x+b_x)\)

那么这个时候的 \(maxdiff=max(|s_1-p|,|s_2-p|)\)

\(dp\) 求可以到的 \(p\) ,暴力

// Author: xiaruize
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int N = 50 + 10;

int n;
int a[N], b[N];
int s, sum;
bitset<500005> bt[55];

void solve()
{
	cin >> n;
	rep(i, 1, n)
	{
		cin >> a[i];
		s += a[i];
	}
	cin >> n;
	rep(i, 1, n)
	{
		cin >> b[i];
		sum += b[i];
	}
	bt[0][0] = 1;
	rep(i, 1, n)
	{
		bt[i] = (bt[i - 1] | (bt[i - 1] << (a[i] + b[i])));
	}
	int res = INF;
	rep(i, 0, 5e5)
	{
		if (bt[n][i])
		{
			// debug(i);
			res = min(res, max(abs(s - i), abs(sum - i)));
		}
	}
	cout << res << endl;
}

#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif

signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
		solve();
#ifndef ONLINE_JUDGE
	cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
	cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
	return 0;
}
posted @ 2024-04-10 10:12  xiaruize  阅读(6)  评论(0)    收藏  举报