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. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself.
Recently Xenia has bought nr red gems, ng green gems and 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, y and 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) — the number of test cases. Then t test cases follow.
The first line of each test case contains three integers n r , n g , n b n_r,n_g,n_b nr,ng,nb ( 1 ≤ n r , n g , n b ≤ 1 e 5 ) (1≤n_r,n_g,n_b≤1e5) (1≤nr,ng,nb≤1e5) — the number of red gems, green gems and blue gems respectively.
The second line of each test case contains nr integers r 1 , r 2 , … , r n r ( 1 ≤ r i ≤ 1 e 9 ) r_1,r_2,…,r_{n_{r}} (1≤r_i≤1e9) r1,r2,…,rnr(1≤ri≤1e9) — ri is the weight of the i-th red gem.
The third line of each test case contains ng integers g 1 , g 2 , … , g n g ( 1 ≤ g i ≤ 1 e 9 ) g_1,g_2,…,g_{n_g} (1≤g_i≤1e9) g1,g2,…,gng(1≤gi≤1e9) — gi is the weight of the 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 e 9 ) b_1,b_2,…,b_{n_b} (1≤bi≤1e9) b1,b2,…,bnb(1≤bi≤1e9) — bi is the weight of the 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
output
14
1999999996000000002
24
24
14
这题感觉比C好想一点,我一下就想到了记录下标,即对所有的 r , g , b r,g,b r,g,b 预处理,求出与之最近的其他两个位置的下标,打个比方对下标为 i i i 的 r r r,左边最近的 g g g 下标为 p o s 1 pos1 pos1,最近的 b b b 下标为 p o s 2 pos2 pos2,右边最近的 g g g 下标为 p o s 3 pos3 pos3,最近的 b b b 下标为 p o s 4 pos4 pos4,那么 a n s = min ( c a l c u l a t e ( a [ p o s 1 ] , a [ i ] , a [ p o s 4 ] ) , c a l c u l a t e ( a [ p o s 2 ] , a [ i ] , a [ p o s 3 ] ) ) ans=\min(calculate(a[pos1],a[i],a[pos4]),calculate(a[pos2],a[i],a[pos3])) ans=min(calculate(a[pos1],a[i],a[pos4]),calculate(a[pos2],a[i],a[pos3])),
c a l c u l a t e ( x , y , z ) calculate(x,y,z) calculate(x,y,z) 表示计算最小值的函数,AC代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;struct node{int k,id;
}p[3*N];ll calculate(int i,int j,int k){return ll(i-j)*ll(i-j)+ll(i-k)*ll(i-k)+ll(j-k)*ll(j-k);
}bool cmp(node a,node b){return a.k<b.k;
}int main(){int t;cin>>t;while(t--){int n1,n2,n3,n;cin>>n1>>n2>>n3;n=n1+n2+n3;int l12[n],l13[n],r12[n],r13[n],l21[n],l23[n],r21[n],r23[n],l31[n],l32[n],r31[n],r32[n];for(int i=0;i<n1;i++) p[i].id=1,cin>>p[i].k;for(int i=n1;i<n1+n2;i++) p[i].id=2,cin>>p[i].k;for(int i=n1+n2;i<n1+n2+n3;i++) p[i].id=3,cin>>p[i].k;sort(p,p+n,cmp);int pos1=-1,pos2=-1,pos3=-1;for(int i=0;i<n;i++){if(p[i].id==1) pos1=i,l12[i]=pos2,l13[i]=pos3;if(p[i].id==2) pos2=i,l21[i]=pos1,l23[i]=pos3;if(p[i].id==3) pos3=i,l31[i]=pos1,l32[i]=pos2;}pos1=-1;pos2=-1,pos3=-1;for(int i=n-1;i>=0;i--){if(p[i].id==1) pos1=i,r12[i]=pos2,r13[i]=pos3;if(p[i].id==2) pos2=i,r21[i]=pos1,r23[i]=pos3;if(p[i].id==3) pos3=i,r31[i]=pos1,r32[i]=pos2;}ll ans=pow(2,63)-1;for(int i=0;i<n;i++){if(p[i].id==1){if(l12[i]!=-1 && r13[i]!=-1) ans=min(ans,calculate(p[l12[i]].k,p[i].k,p[r13[i]].k));if(l13[i]!=-1 && r12[i]!=-1) ans=min(ans,calculate(p[l13[i]].k,p[i].k,p[r12[i]].k));}else if(p[i].id==2){if(l21[i]!=-1 && r23[i]!=-1) ans=min(ans,calculate(p[l21[i]].k,p[i].k,p[r23[i]].k));if(l23[i]!=-1 && r21[i]!=-1) ans=min(ans,calculate(p[l23[i]].k,p[i].k,p[r21[i]].k));}else{if(l31[i]!=-1 && r32[i]!=-1) ans=min(ans,calculate(p[l31[i]].k,p[i].k,p[r32[i]].k));if(l32[i]!=-1 && r31[i]!=-1) ans=min(ans,calculate(p[l32[i]].k,p[i].k,p[r31[i]].k));}}cout<<ans<<endl;}
}
Codeforces Round #635 (Div. 2) D.Xenia and Colorful Gems相关推荐
- 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)^ ...
- Codeforces Round #635 (Div. 1) C. Kaavi and Magic Spell 区间dp
传送门 文章目录 题意: 思路: 题意: 给你两个串s,ts,ts,t,每次都可以从sss的开头拿一个字符放到AAA串的开头或结尾,问最终有多少种方案使得ttt是AAA的前缀,注意sss不必全部拿完. ...
- 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 ...
- Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)
题目链接: B. Xenia and Hamming 题意: 要求找到复制后的两个字符串中不同样的字符 思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在还有一个串中反复字符的个数 CO ...
- Codeforces Round #197 (Div. 2): C. Xenia and Weights(记忆化搜索)
题意: 先输入一个长度为10的01串,第i个数字为1表示你有重量为i的砝码无数个,第i个数字为0表示你没有重量为i的砝码,你需要按照以下规则在一个一开始平衡的天平上放上m个砝码 第1个砝码放在天平左边 ...
- Codeforces Round #197 (Div. 2): D. Xenia and Bit Operations(线段树)
题意:定义一个长度2^n的序列{a1, a2-an},序列相邻两个元素或运算之后再进行异或运算再进行或运算(两种运算交替进行)直到只剩下一个数字,这个数字即为序列的值,输入第一行两个数n,m表示序列的 ...
- [Codeforces Round #635 (div2)]1337D - Xenia and Colorful Gems[二分][枚举]
1337D - Xenia and Colorful Gems[二分][枚举] time limit per test memory limit per test input output 3 sec ...
- Codeforces Round #506 (Div. 3)
Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...
- Codeforces Round #563 (Div. 2)/CF1174
Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...
最新文章
- Docker学习(六)-----Docker数据卷
- 专业人士提升数据中心职业生涯的6件事
- Unity动画系统经验谈:换装系统与骨骼调节
- “约见”面试官系列之常见面试题第四十四篇之webpack打包原理解析?(建议收藏)
- 分页标签commons.tld,NavigationTag,Page
- TypeScript -脚本编程语言
- ACM-ICPC 2017 Asia Xi'an A XOR (线性基+线段树思想)
- 手机上怎么去掉a 标签中的img点击时的阴影?
- 华为云计算hcip证书有效期_华为云计算HCIP V4.0认证要发布了!
- form表单提交中的input,button,submit
- Unity UI层级管理框架
- win8.1安装密钥
- Crystal Reports(水晶报表)安装及拉(PULL)模式/推(PUSH)模式的使用
- 前端开发应知网站(转载)
- 最近用到的shell命令
- unity中计算向量的模长和归一化向量
- 《大话西游》经典对白
- 我眼中的匈牙利命名法
- T13735 fateice-string洛谷八连测2
- Springboot快递代取系统的设计与实现3i0v9计算机毕业设计-课程设计-期末作业-毕设程序代做