CSU 1165: Nearest Numbers

举报
用户已注销 发表于 2021/11/19 05:57:03 2021/11/19
1k+ 0 0
【摘要】 题目: 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 wa...

题目:

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

AC代码:


      #include<iostream>
      #include<algorithm>
      using namespace std;
      long long x[3][100001];
      long long f(int a, int b, int c)
      {
     	long long r = 1;
      	r = (r << 63) - 1;
     	for (int i = 1, j = 1, k = 1; i <= x[a][0]; i++)
      	{
     		while (j < x[b][0] && x[a][i] >= x[b][j+1])j++;
     		while (k < x[c][0] && x[a][i] > x[c][k])k++;
      		r = min(r,(x[a][i] - x[b][j])*(x[a][i] - x[b][j]) + (x[b][j] - x[c][k])*(x[b][j] - x[c][k]) + (x[a][i] - x[c][k])*(x[a][i] - x[c][k]));
      	}
     	return r;
      }
      int main()
      {
     	while (cin >> x[0][0] >> x[1][0] >> x[2][0])
      	{
     		for (int i = 0; i < 3; i++)for (int j = 1; j <= x[i][0]; j++)cin >> x[i][j];
     		long long r1 = min(f(0, 1, 2), f(0, 2, 1));
     		long long r2 = min(f(1, 0, 2), f(1, 2, 0));
     		long long r3 = min(f(2, 0, 1), f(2, 1, 0));
      		cout << min(min(r1, r2), r3) << endl;
      	}
     	return 0;
      }
  
 

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/79726353

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。