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

48 篇文章 0 订阅
5 篇文章 0 订阅

D. Xenia and Colorful Gems

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

f3da718b55ccc1ed3494e6615f1db496fd70b0ad.pnguploading.4e448015.gif转存失败重新上传取消

 

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

Copy

(copy)

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

Copy

(copy)

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

思路:

对每个ri找g中刚好大于等于和刚好小于等于的数,假设分别是gx,gy,对gx,我们在b找刚好大于等于(ri+gx)/2的数和刚好小于等于(ri+gx)/2的数,假设分别是bx1,by1。。。
这样我们会得到(ri,gx,bx1)(ri,gx,by1)(ri,gy,bx2)(ri,gy,by2)这四组,取最小的

遍历所有ri得到的最小的最小值就是答案
最后6种组合都找一遍,写个函数直接复制就好了

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
typedef long long ll;
ll r[N],g[N],b[N];
ll js(ll x,ll y,ll z)
{
    return (x-y)*(x-y)+(x-z)*(x-z)+(y-z)*(y-z);
}
ll pd(ll x,ll y,ll c[],ll sc)
{
    ll cx,cy;
    cx=lower_bound(c+1,c+1+sc,(x+y)/2ll)-c;
    cy=cx-1;
    if(cx==sc+1) cx=cy;
    if(cy==0) cy=cx;
    ll mi=js(x,y,c[cx]);
    mi=min(mi,js(x,y,c[cy]));
    return mi;
}
ll slove(ll a[],ll sa,ll b[],ll sb,ll c[],ll sc)
{
    ll mi=5e18;
    for(ll i=1;i<=sa;i++)
    {
        ll bx,by;
        bx=lower_bound(b+1,b+1+sb,a[i])-b;
        by=bx-1;
        if(bx==sb+1) bx=by;
        if(by==0) by=bx;
        mi=min(pd(a[i],b[bx],c,sc),mi);
        mi=min(pd(a[i],b[by],c,sc),mi);
    }
    return mi;
}
int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        ll nr,ng,nb;
        scanf("%lld %lld %lld",&nr,&ng,&nb);
        for(ll i=1;i<=nr;i++)
        {
            scanf("%lld",&r[i]);
        }
        for(ll i=1;i<=ng;i++)
        {
            scanf("%lld",&g[i]);
        }
        for(ll i=1;i<=nb;i++)
        {
            scanf("%lld",&b[i]);
        }
        sort(r+1,r+1+nr);
        sort(g+1,g+1+ng);
        sort(b+1,b+1+nb);
        ll mi=5e18;
        mi=min(slove(r,nr,g,ng,b,nb),mi);
        mi=min(slove(r,nr,b,nb,g,ng),mi);
        mi=min(slove(g,ng,r,nr,b,nb),mi);
        mi=min(slove(g,ng,b,nb,r,nr),mi);
        mi=min(slove(b,nb,g,ng,r,nr),mi);
        mi=min(slove(b,nb,r,nr,g,ng),mi);
        printf("%lld\n",mi);
    }
}


 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值