Xenia and Colorful Gems CodeForces 1337D[binary search +data structures implementation+ math sortings]

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 (xy)2+(yz)2+(zx)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 (1t1001≤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 (1nr,ng,nb1051≤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 (1ri1091≤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 (1gi1091≤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 (1bi1091≤bi≤109)  — bibi is the weight of the ii-th blue gem.

It is guaranteed that nr105∑nr≤105, ng105∑ng≤105, nb105∑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 (xy)2+(yz)2+(zx)2=(76)2+(64)2+(47)2=14(x−y)2+(y−z)2+(z−x)2=(7−6)2+(6−4)2+(4−7)2=14.

ACcode#1

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

 ACcode#2

#include<bits/stdc++.h>
using namespace std;
int read() {
    char c=getchar();while(!isdigit(c)) c=getchar();
    int num=0;while(isdigit(c)) num=num*10+c-'0',c=getchar();
    return num;
}
inline long long func(int a, int b, int c) {
    return 1ll*(a-b)*(a-b)+1ll*(b-c)*(b-c)+1ll*(c-a)*(c-a);
}
inline long long solve(int a[], int b[], int c[], int n1, int n2, int n3) {
    long long ans = 5000000000000000000;
    int p = 0, q = 0;
    for (int i = 1; i <= n1; i++) {
        while (q <= n2 && b[q] < a[i]) ++q;
        while (p < n3 && c[p+1] <= a[i]) ++p;
        if (p && q <= n2) ans = min(ans, func(a[i], b[q], c[p]));
    }
    return ans;
}
int a[100001], b[100001], c[100001];
int main() {
    int t = read();
    while (t--) {
        int n1, n2, n3;
        n1 = read(), n2 = read(), n3 = read();
        for (int i = 1; i <= n1; i++) a[i] = read();
        for (int i = 1; i <= n2; i++) b[i] = read();
        for (int i = 1; i <= n3; i++) c[i] = read();
        sort(a + 1, a + n1 + 1);
        sort(b + 1, b + n2 + 1);
        sort(c + 1, c + n3 + 1);
        long long ans = 5000000000000000000;
        ans = min(ans, solve(a, b, c, n1, n2, n3));
        ans = min(ans, solve(a, c, b, n1, n3, n2));
        ans = min(ans, solve(b, a, c, n2, n1, n3));
        ans = min(ans, solve(b, c, a, n2, n3, n1));
        ans = min(ans, solve(c, a, b, n3, n1, n2));
        ans = min(ans, solve(c, b, a, n3, n2, n1));
        cout << ans << endl;
    }
}
原文地址:https://www.cnblogs.com/xxxsans/p/12712419.html