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

time limit per test memory limit per test input output
3 seconds 256 megabytes standard input standard 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 (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 ) t (1≤t≤100) t(1≤t≤100) — 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​(1≤nr​,ng​,nb​≤105) — 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​​(1≤ri​≤109) — 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​​(1≤gi​≤109) — 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​​(1≤bi​≤109) — 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 ∑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

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 (x−y)2+(y−z)2+(z−x)2=(7−6)2+(6−4)2+(4−7)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((ri​−gj​)2+(gj​−bk​)2+(ri​−bk​)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 1≤i≤nr​,1≤j≤ng​,1≤k≤nb​

做法:
现在看到数字大的题就有种下意识的二分
显然如果我们确定了第一个数,例如确定 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} bk−1​ 的结果,取 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;
}

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

  1. CodeForces - 1337D Xenia and Colorful Gems(二分)

    题目链接:点击查看 题目大意:给出三个序列分别记为 a,b,c,现在要求分别从三个序列中找出 x , y , z ,使得 ( x - y )^2 + ( y - z )^2 + ( y - z )^2 ...

  2. Xenia and Colorful Gems(二分--思维)

    给定三个数组a,b,c. 要求从每个数字取一个数,使得两两之差和最小. 求出这个数. 我 又 懵 逼 了 . 我 是 会 O ( n 3 ) 的 暴 力 啊 , 怎 么 办 . 我又懵逼了.我是会O( ...

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

    传送门 文章目录 题意: 思路: 题意: 给你三个数组a,b,ca,b,ca,b,c,让你从每个数组中选择一个数x,y,zx,y,zx,y,z,使得(x−y)2+(x−z)2+(y−z)2(x-y)^ ...

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

    Codeforces Round #635 (Div. 2) D.Xenia and Colorful Gems 题目链接 Xenia is a girl being born a noble. Du ...

  5. Codeforces Round #635 (Div. 2)(A~D)题解

    Codeforces #635 A~D A.Ichihime and Triangle B.Kana and Dragon Quest game C.Linova and Kingdom D.Xeni ...

  6. Codeforces Round #635 (Div. 1) C. Kaavi and Magic Spell 区间dp

    传送门 文章目录 题意: 思路: 题意: 给你两个串s,ts,ts,t,每次都可以从sss的开头拿一个字符放到AAA串的开头或结尾,问最终有多少种方案使得ttt是AAA的前缀,注意sss不必全部拿完. ...

  7. Educational Codeforces Round 111 (Rated for Div. 2) E. Stringforces 二分 + 状压dp

    传送门 文章目录 题意: 思路: 题意: 给你一个串,只包含前kkk个字母和???,定义fif_ifi​表示第iii个字母在串中出现的最长连续长度,你现在需要将???替换为前kkk个字母,使得mini ...

  8. Codeforces Round #271 (Div. 2) C. Captain Marmot (暴力枚举+正方形判定)

    题目链接:Codeforces Round #271 (Div. 2) C. Captain Marmot 题意:给4行数据,每行2个点.(x,y).(a,b).意思是(x,y)绕(a,b)逆时针旋转 ...

  9. codeforces Round#429 (Div2)

    2017-08-20 10:00:37 writer:pprp 用头文件#include <bits/stdc++.h>很方便 A. Generous Kefa codeforces 84 ...

最新文章

  1. 测试脚本的实用性:谈嵌入式系统在型式试验中的脚本应用
  2. sqlserver excel,txt,access等文件的互導
  3. shell脚本实例-判断主机存活 以及企业备份方案
  4. ecos内核概览--bakayi译
  5. Planning Strategy 和Requirement type的思考
  6. 老板让我用少量样本 finetune 模型,我还有救吗?急急急,在线等!
  7. php如何直接使用iview,iview 使用总结
  8. 那些年,翻过山,趟过河,挖了山丘,黑了河沟,终于还是遇到了——跨服务器查询...
  9. 页面可用性之浏览器默认字体与CSS 中文字体
  10. 如何在点击a标签下载文件的时候通过JavaScript动态的修改文件的名称?
  11. java实现字符串MD5加密32位大小写
  12. 制作网页头部用html,HTML网页头部代码实例详解_HTML/Xhtml_网页制作
  13. 中介模式(python实现2)
  14. 9个设计师都在用的图片素材网站,风格齐全,高清免费
  15. 计算机输入出设备课件,《电脑输入设备》PPT课件.ppt
  16. 数据中心如何向私有云转变
  17. 从摆地摊到全球网商10强
  18. JavaFX桌面应用开发-GridPane(网格布局)
  19. 计算机网络:常见的计网面试题整理(一)
  20. 查看安卓手机文件系统方法

热门文章

  1. Kafka副本同步机制
  2. java计算课程学分绩点,一个简单的计算选修课程绩点的程序,欢迎大家指点下.
  3. Android 农历日历算法
  4. oracle待摊费用改为一次性摊销,长期待摊费用可以一次性转入损益吗
  5. z-index的理解
  6. 计算机硬件系统及组装ppt,《计算机硬件系统及》PPT课件.ppt
  7. 四、视觉SLAM所需基本知识——矩阵论
  8. oracle分页rowid,oracle中rowid高速分页
  9. 毕业论文数据分析方法分类汇总
  10. 笔记本卡顿不流畅是什么原因_电脑卡顿不流畅是什么原因?PC卡顿原来可以这么解决...