码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-04-16 20:55:59      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:div   枚举   xen   cout   col   turn   main   include   force   

#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;
}

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

标签:div   枚举   xen   cout   col   turn   main   include   force   

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12715583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!