考虑二分。
枚举每个数组为x,然后去二分出来y的值,在二分第三个数组为z的值取最小即可。
注意一下这里的二分,我们要二分出来第一个≥x的数为y,在二分出来第一个≥y的数为z
这样不一定最优,还要考虑比他小的第一个。假设y的位置为pos z的位置为pos1
那么组合的就是 b[pos] c[pos1] b[pos] c[pos1-1]
然后改变pos为pos-1 则需要重新二分pos1的位置即可
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1 << 18], b[1 << 18], c[1 << 18];
ll te[1 << 18];
ll get(int i, int j, int k)
{
return (a[i] - b[j]) * (a[i] - b[j]) + (b[j] - c[k]) * (b[j] - c[k]) + (c[k] - a[i]) * (c[k] - a[i]);
}
int main()
{
int t;
cin >> t;
while (t--)
{
int na, nb, nc;
cin >> na >> nb >> nc;
for (int i = 1; i <= na; i++)
cin >> a[i];
for (int i = 1; i <= nb; i++)
cin >> b[i];
for (int i = 1; i <= nc; i++)
cin >> c[i];
sort(a + 1, a + 1 + na);
sort(b + 1, b + 1 + nb);
sort(c + 1, c + 1 + nc);
ll ans = 3e18;
for (int tt = 1; tt <= 3; tt++)
{
if (tt == 2)
{
for (int i = 1; i <= na; i++)
te[i] = a[i];
for (int i = 1; i <= nb; i++)
a[i] = b[i];
for (int i = 1; i <= na; i++)
b[i] = te[i];
swap(na, nb);
}
if (tt == 3)
{
for (int i = 1; i <= na; i++)
te[i] = a[i];
for (int i = 1; i <= nc; i++)
a[i] = c[i];
for (int i = 1; i <= na; i++)
c[i] = te[i];
swap(na, nc);
}
for (int i = 1; i <= na; i++)
{
int pos = lower_bound(b + 1, b + 1 + nb, a[i]) - b;
int pos1 = lower_bound(c + 1, c + 1 + nc, b[pos]) - c;
if (pos >= 1 && pos1 >= 1 && pos <= nb && pos1 <= nc)
ans = min(ans, get(i, pos, pos1));
if (pos1 > 1 && pos >= 1 && pos <= nb && pos1 <= nc)
ans = min(ans, get(i, pos, pos1 - 1));
pos--;
pos1 = lower_bound(c + 1, c + 1 + nc, b[pos]) - c;
if (pos >= 1 && pos1 >= 1 && pos <= nb && pos1 <= nc)
ans = min(ans, get(i, pos, pos1));
if (pos1 > 1 && pos >= 1 && pos <= nb && pos1 <= nc)
ans = min(ans, get(i, pos, pos1 - 1));
pos = lower_bound(c + 1, c + 1 + nc, a[i]) - c;
pos1 = lower_bound(b + 1, b + 1 + nb, c[pos]) - b;
if (pos >= 1 && pos1 >= 1 && pos1 <= nb && pos <= nc)
ans = min(ans, get(i, pos1, pos));
if (pos1 > 1 && pos >= 1 && pos1 <= nb && pos <= nc)
ans = min(ans, get(i, pos1 - 1, pos));
pos--;
pos1 = lower_bound(b + 1, b + 1 + nb, c[pos]) - b;
if (pos >= 1 && pos1 >= 1 && pos1 <= nb && pos <= nc)
ans = min(ans, get(i, pos1, pos));
if (pos1 > 1 && pos >= 1 && pos1 <= nb && pos <= nc)
ans = min(ans, get(i, pos1 - 1, pos));
}
}
cout << ans << endl;
}
return 0;
}