系统相关
首页 > 系统相关> > Xenia and Colorful Gems CodeForces - 1337D[binary search +data structures implementation+ math sorti

Xenia and Colorful Gems CodeForces - 1337D[binary search +data structures implementation+ math sorti

作者:互联网

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.

snoop dooogg~~~ 

Recently Xenia has bought nrnr red gems, ngng green gems and nbnb 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 xx, yy and zz, Xenia wants to find the minimum value of (x−y)2+(y−z)2+(z−x)2(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 tt (1≤t≤1001≤t≤100)  — the number of test cases. Then tt test cases follow.

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

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

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

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

It is guaranteed that ∑nr≤105∑nr≤105, ∑ng≤105∑ng≤105, ∑nb≤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(x−y)2+(y−z)2+(z−x)2=(7−6)2+(6−4)2+(4−7)2=14.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 4e18+10;
const int mod = 1000000007;
const int mx = 200010; //check the limits, dummy
typedef pair<int, int> pa;
const double PI = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
#define swa(a,b) a^=b^=a^=b
#define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) ((x)&(x-1))
#define mkp make_pair
void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
ll  m, n,t,y,z,k,sum=0,ans=0;
ll fuck(ll x, ll y, ll z) {
    return (x - y) * (x - y) + (y - z) * (y - z) + (x - z) * (x - z);
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t--)
    {
        ll candy[3];
        vector<ll>v[3];
        cin >> candy[0] >> candy[1] >> candy[2];
        re(p, 0, 3) {
            v[p].resize(candy[p]);
            re(i, 0, candy[p])cin >> v[p][i];
            sort(v[p].begin(), v[p].end());
        }
        ans = inf;
        re(p1, 0, 3) {
            re(p2, 0, 3) {
                re(p3, 0, 3) {
                    if (p1 == p2 || p2 == p3 || p1 == p3)continue;
                    for (auto x : v[p1]) {
                        auto i1 = lower_bound(v[p2].begin(), v[p2].end(), x);
                        auto i2 = upper_bound(v[p3].begin(), v[p3].end(), x);
                        if (i1 != v[p2].end() && i2 != v[p3].begin()) {
                            y = *i1;
                            i2--;
                            z = *i2;
                            ans = min(ans, fuck(x, y, z));
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 

标签:Gems,binary,search,weight,ll,Xenia,gems,test,gem
来源: https://www.cnblogs.com/xxxsans/p/12712419.html