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)/~

发布了668 篇原创文章 · 获赞 118 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105599843