[Codeforces Round #635 (div2)]1337D - Xenia and Colorful Gems[二分][枚举]

1337D - Xenia and Colorful Gems[二分][枚举]

time limit per testmemory limit per testinputoutput
3 seconds256 megabytesstandard inputstandard output

Description:

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 n r n_r nr red gems, n g n_g ng green gems and n b n_b 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 x x, y y y and z z z, 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 (xy)2+(yz)2+(zx)2. As her dear friend, can you help her?

Input

The first line contains a single integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1t100) — the number of test cases. Then t t t test cases follow.

The first line of each test case contains three integers n r , n g , n b ( 1 ≤ n r , n g , n b ≤ 1 0 5 ) n_r,n_g,n_b (1≤n_r,n_g,n_b≤10^5) nr,ng,nb(1nr,ng,nb105) — the number of red gems, green gems and blue gems respectively.

The second line of each test case contains n r n_r nr integers r 1 , r 2 , … , r n r ( 1 ≤ r i ≤ 1 0 9 ) r_1,r_2,…,r_{n_r} (1≤r_i≤10^9) r1,r2,,rnr(1ri109) — r_i is the weight of the i i i-th red gem.

The third line of each test case contains n g n_g ng integers g 1 , g 2 , … , g n g ( 1 ≤ g i ≤ 1 0 9 ) g_1,g_2,…,g_{n_g} (1≤g_i≤10^9) g1,g2,,gng(1gi109) g i g_i gi is the weight of the i i i-th green gem.

The fourth line of each test case contains nb integers b 1 , b 2 , … , b n b ( 1 ≤ b i ≤ 1 0 9 ) b_1,b_2,…,b_{n_b} (1≤b_i≤10^9) b1,b2,,bnb(1bi109) b i b_i bi is the weight of the i i i-th blue gem.

It is guaranteed that ∑ n r ≤ 1 0 5 , ∑ n g ≤ 1 0 5 , ∑ n b ≤ 1 0 5 ∑n_r≤10^5, ∑n_g≤10^5, ∑n_b≤10^5 nr105,ng105,nb105 (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

Example output

14
1999999996000000002
24
24
14

Hit

In the first test case, Xenia has the following gems:
在这里插入图片描述
If she picks the red gem with weight 7 7 7, the green gem with weight 6 6 6, and the blue gem with weight 4 4 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 (xy)2+(yz)2+(zx)2=(76)2+(64)2+(47)2=14.


分析:
题意:
m i n ( ( r i − g j ) 2 + ( g j − b k ) 2 + ( r i − b k ) 2 ) min((r_i - g_j)^2 + (g_j- b_k)^2 + (r_i - b_k)^2) min((rigj)2+(gjbk)2+(ribk)2)
其中 1 ≤ i ≤ n r , 1 ≤ j ≤ n g , 1 ≤ k ≤ n b 1 \leq i \leq n_r, 1 \leq j \leq n_g, 1 \leq k \leq n_b 1inr,1jng,1knb

做法:
现在看到数字大的题就有种下意识的二分
显然如果我们确定了第一个数,例如确定 r i r_i ri
那么要使得结果最小,剩下的两个数也可以基本确定
找到 第一个大于等于 r i r_i ri g j g_j gj
g j g_j gj 如果固定下来,那么 b k b_k bk 必然是离 r i + g j 2 \frac{r_i+g_j}{2} 2ri+gj 最近的一个数
r i + g j r_i+g_j ri+gj 是因为 b k b_k bk 的选择对这两边的值都有影响
这里有一个问题,离这个值最近,但是不代表大于等于这个值的数一定是最近的
因此还要求一下跟 b k − 1 b_{k-1} bk1 的结果,取 m i n min min
这样看来,求 g j g_j gj 的时候是不是也有这样的可能
其实要取的是前一个才是最近的
这边可以暴力枚举以 r i , g j , b k r_i, g_j, b_k ri,gj,bk 哪一个作为标准来计算
这样就可以遍历到


Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 5;
#define min(a, b)   ((a)>(b)?(b):(a))

vector<ll> v[3];
int n[3];

ll solve(ll a, ll b, ll c) {
    return (a - b) * (a - b) + (a - c) * (a - c) + (b - c) * (b - c);
}

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        ll x, ans = 4e18;
        for(int i = 0; i < 3; ++i) {
            scanf("%d", &n[i]); v[i].clear();
        }
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < n[i]; ++j) {
                scanf("%lld", &x); v[i].push_back(x);
            }
        }
        for(int i = 0; i < 3; ++i)
            sort(v[i].begin(), v[i].end());

        int t1, t2;
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) {
                for(int k = 0; k < 3; ++k) {
                    if(i == j || i == k || j == k)  continue;
                    for(auto t : v[i]) {
                        t1 = lower_bound(v[j].begin(), v[j].end(), t) - v[j].begin();
                        t1 = min(t1, n[j] - 1);
                        t2 = lower_bound(v[k].begin(), v[k].end(), (t + v[j][t1] + 1) / 2) - v[k].begin();
                        t2 = min(t2, n[k] - 1);
                        ans = min(ans, solve(t, v[j][t1], v[k][t2]));
                        t2 = max(t2 - 1, 0);
                        ans = min(ans, solve(t, v[j][t1], v[k][t2]));
                        // printf("%lld %lld %lld\n", t0, v1[t1], v2[t2]);
                        // puts("");
                    }
                }
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}







内容概要:《2024年中国物联网产业创新白皮书》由深圳市物联网产业协会与AIoT星图研究院联合编制,汇集了全国30多个省市物联网组织的智慧。白皮书系统梳理了中国物联网产业的发展历程、现状及未来趋势,涵盖了物联网的概念、产业结构、市场规模、投融资情况、面临的问题与机遇。书中详细分析了感知层、传输层、平台层及应用层的关键技术,探讨了智慧城市、智能工业、车联网、智慧医疗等九大产业物联网应用领域,以及消费物联网的发展特征与热门单品。此外,白皮书还关注了物联网数据安全、法规遵从、人才短缺等挑战,并提出了相应的解决方案。 适用人群:物联网从业者、企业决策者、政策制定者及相关研究机构。 使用场景及目标:①帮助从业者深入了解物联网产业的现状和发展趋势;②为企业决策者提供战略规划依据;③为政策制定者提供政策支持和法规制定参考;④为研究机构提供详尽的数据和案例支持。 其他说明:白皮书不仅限于技术科普,更从宏观角度结合市场情况,多维度讨论了物联网产业生态,旨在为物联网企业、从业者找到最适合的技术应用场景,促进产业健康发展。报告还特别鸣谢了参与市场调研的企业,感谢他们提供的宝贵行业信息。由于时间和资源的限制,报告可能存在信息不充分之处,欢迎各界人士提出宝贵意见。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值