Codeforces Round #635 (Div. 2) D-Xenia and Colorful Gems 二分

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5+10;
typedef long long ll;
ll t, n1, n2, n3;
ll a[N], b[N], c[N];
//分别固定一个数 设为a[i]
//然后从 b 和 c 中找一个小于等于他的 和 一个大于等于他的 
//然后枚举每个数字的每个数 
ll solve(ll a[], ll n1, ll b[], ll n2, ll c[] , ll n3)
{
	ll ans = 2e18, x, y, z;
	for(int i = 1; i <= n1; i++)
	{
		x = a[i];
		int l = 1, r = n2;
		while(l < r)
		{
			int mid = l + r >>1;
			if(b[mid] >= x)
				r = mid;
			else
				l = mid +1;
		}
		if(b[l] >= x)
			y=b[l];
		else
			continue;
		l = 1, r = n3;
		while(l < r)
		{
			int mid = l + r + 1 >>1;
			if(c[mid] <= x)
				l = mid;
			else
				r = mid - 1;
		}
		if(c[l] <= x)
			z = c[l];
		else
			continue;
		ans=min(ans, (x - y) * (x - y) +(x - z) * (x - z) + (y - z) * (y - z));
	}
	return ans;
}
void solve()
{
	cin >> n1 >> n2 >> n3;
	for(int i = 1; i <= n1; i++)
		cin >> a[i];
	for(int i = 1; i <= n2; i++)
		cin >> b[i];
	for(int i = 1; i <= n3; i++)
		cin >> c[i];
	sort(a + 1, a + 1 + n1);
	sort(b + 1, b + 1 + n2);
	sort(c + 1, c + 1 + n3);
	ll ans = 2e18;
	ans = min(ans, solve(a, n1, b, n2, c, n3));
	ans = min(ans, solve(a, n1, c, n3, b, n2));
	ans = min(ans, solve(b, n2, a, n1, c, n3));
	ans = min(ans, solve(b, n2, c, n3, a, n1));
	ans = min(ans, solve(c, n3, b, n2, a, n1));
	ans = min(ans, solve(c, n3, a, n1, b, n2));
	cout << ans << endl;
}
int main()
{
	cin >> t;
	while(t --)
		solve();
	return 0;
}
上一篇:P1541 乌龟棋 暴力DP


下一篇:同符号数学运算