Nearest Numbers



Description

 Given three integer sequences which have been sorted in ascending order,pick one integer from each sequence and we can get three integers.we want these three integers to be nearest to each other.Assuming these three integers as a,b,c, they are nearest to each other means d=(a-b)2+(b-c)2+(c-a)2 is the smallest.

Input

 There are many test cases. For each test case,the first line are three integers la,lb,lc, they let you know the length of each sequence. The following three lines separately have la,lb,lc integers,give you the integers in each sequence. 1<=la,lb,lc<=10^6 and All integers in the sequences are in the range of [-10^9,10^9].

Output

For each test case, just print one integer : the smallest d as mentioned above.

Sample Input

10 10 10
1 2 3 4 5 6 7 8 9 10
2 3 6 8 10 12 14 16 18 20
3 5 7 9 11 12 14 16 18 20

Sample Output

0

HINT

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int maxn = 1 << 20;
typedef long long LL;
LL a[maxn], b[maxn], c[maxn];
inline LL min(LL x, LL y)
{return x < y ? x : y;}
inline LL cal(LL x, LL y, LL z)
{return (x - y) * (x - y) + (x - z) * (x - z) + (z - y) * (z - y);}
int main()
{
 LL ans, tmpa, tmpb, tmpc;
 int n1, n2, n3, i, j, k;
// freopen("hehe.txt", "r", stdin);
 while(scanf("%d%d%d", &n1, &n2, &n3) != EOF)
 {
  for(i = 0; i < n1; ++ i) scanf("%lld", &a[i]);
  for(i = 0; i < n2; ++ i) scanf("%lld", &b[i]);
  for(i = 0; i < n3; ++ i) scanf("%lld", &c[i]);
  ans = ((LL)1 << 63) - 1;
  i = j = k = 0;
  while(i < n1 && j < n2 && k < n3 && ans > 0)
  {
   ans = min(ans, cal(a[i], b[j], c[k]));
   tmpa = cal(a[i + 1], b[j], c[k]);
   tmpb = cal(a[i], b[j + 1], c[k]);
   tmpc = cal(a[i], b[j], c[k + 1]);
   if(tmpa < tmpb && tmpa < tmpc) ++ i;
   else if(tmpb < tmpc) ++ j;
   else ++ k;
  }
  printf("%lld\n", ans);
 }
 return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值