Xenia and Colorful Gems CodeForces - 1337D(数学+二分)

Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself.

Recently Xenia has bought nr red gems, ng green gems and nb blue gems. Each of the gems has a weight.

Now, she is going to pick three gems.

Xenia loves colorful things, so she will pick exactly one gem of each color.

Xenia loves balance, so she will try to pick gems with little difference in weight.

Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x−y)2+(y−z)2+(z−x)2. As her dear friend, can you help her?

Input
The first line contains a single integer t (1≤t≤100) — the number of test cases. Then t test cases follow.

The first line of each test case contains three integers nr,ng,nb (1≤nr,ng,nb≤105) — the number of red gems, green gems and blue gems respectively.

The second line of each test case contains nr integers r1,r2,…,rnr (1≤ri≤109) — ri is the weight of the i-th red gem.

The third line of each test case contains ng integers g1,g2,…,gng (1≤gi≤109) — gi is the weight of the i-th green gem.

The fourth line of each test case contains nb integers b1,b2,…,bnb (1≤bi≤109) — bi is the weight of the i-th blue gem.

It is guaranteed that ∑nr≤105, ∑ng≤105, ∑nb≤105 (the sum for all test cases).

Output
For each test case, print a line contains one integer — the minimum value which Xenia wants to find.

Example
Input
5
2 2 3
7 8
6 3
3 1 4
1 1 1
1
1
1000000000
2 2 2
1 2
5 4
6 7
2 2 2
1 2
3 4
6 7
3 4 1
3 2 1
7 3 3 4
6
Output
14
1999999996000000002
24
24
14
Note
In the first test case, Xenia has the following gems:

If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x−y)2+(y−z)2+(z−x)2=(7−6)2+(6−4)2+(4−7)2=14.
思路:当三个数字,他们三个差不多大的时候,上面的那个式子才会尽可能的小。选定的这三个数字一定有大有小,因此我们固定一个数组,去另外两个数组中,一个数组寻找第一个大于等于它的数字,另一个去寻找第一个小于等于它的数字,然后按照上面那个答案取最小值。一共是三个数组,因此一共有六种组合。O(6nlogn).
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
ll r[maxx],g[maxx],b[maxx];
int nr,ng,nb;

inline ll judge(ll a[],int na,ll b[],int nb,ll c[],int nc)
{
	ll x,y,z;
	ll ans=3e18;
	for(int i=1;i<=na;i++)
	{
		x=a[i];
		int pos1=lower_bound(b+1,b+nb+1,x)-b;
		int pos2=upper_bound(c+1,c+nc+1,x)-c;
		if(pos2==1||pos1>nb) continue;
		pos2--;
		y=b[pos1];
		z=c[pos2];
		ans=min(ans,((x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y)));
	}
	return ans;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&nr,&ng,&nb);
		for(int i=1;i<=nr;i++) cin>>r[i];
		for(int i=1;i<=ng;i++) cin>>g[i];
		for(int i=1;i<=nb;i++) cin>>b[i];
		sort(g+1,g+1+ng);
		sort(b+1,b+1+nb);
		sort(r+1,r+1+nr);
		ll ans=3e18;
		ans=min(ans,judge(r,nr,g,ng,b,nb));
		ans=min(ans,judge(r,nr,b,nb,g,ng));
		ans=min(ans,judge(g,ng,r,nr,b,nb));
		ans=min(ans,judge(g,ng,b,nb,r,nr));
		ans=min(ans,judge(b,nb,r,nr,g,ng));
		ans=min(ans,judge(b,nb,g,ng,r,nr));
		cout<<ans<<endl;
	}
	return 0;
}

卡了很久这个题,有点难受。。
努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值