Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.

Input

There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.

Output

For each case, firstly you have to print the case number as the form “Case d:”, then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print “YES”, otherwise print “NO”.

Sample Input

3 3 3

1 2 3

1 2 3

1 2 3

3

1

4

10

Sample Output

Case 1:

NO

YES

NO

  1. //该题的思想是先合并前两组,然后用题目中的x减去第3组的值
  2. //然后在合并组里面二分查找,看是否能找到一个值与x减去第三组的值相等
  3. #include<map>
  4. #include<queue>
  5. #include<stack>
  6. #include<vector>
  7. #include<math.h>
  8. #include<cstdio>
  9. #include<sstream>
  10. #include<numeric>//STL数值算法头文件
  11. #include<stdlib.h>
  12. #include<string.h>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<functional>//模板类头文件
  16. using namespace std;
  17. const int INF=1e9+7;
  18. const int maxn=510;
  19. typedef long long ll;
  20. int l,n,m,S;
  21. int a[maxn],b[maxn],c[maxn],ab[maxn*maxn];
  22. int BinarySearch(int ab[],int h,int t)//二分查找
  23. {
  24. int left=0;
  25. int right=h-1;
  26. int mid=(left+right)/2;
  27. while(left<=right)
  28. {
  29. mid=(left+right)/2;
  30. if(ab[mid]==t)
  31. return 1;
  32. else if(ab[mid]>t)
  33. right=mid-1;
  34. else if(ab[mid]<t)
  35. left=mid+1;
  36. }
  37. return 0;
  38. }
  39. int main()
  40. {
  41. int cot=1;
  42. int i,j,k,h,x;
  43. while(scanf("%d %d %d",&l,&n,&m)!=EOF)
  44. {
  45. h=0;
  46. for(i=0; i<l; i++)
  47. scanf("%d",&a[i]);
  48. for(j=0; j<n; j++)
  49. scanf("%d",&b[j]);
  50. for(k=0; k<m; k++)
  51. scanf("%d",&c[k]);
  52. for(i=0; i<l; i++)
  53. for(j=0; j<n; j++)
  54. ab[h++]=a[i]+b[j];
  55. sort(ab,ab+h);
  56. printf("Case %d:\n",cot++);
  57. scanf("%d",&S);
  58. for(int s=0; s<S; s++)
  59. {
  60. scanf("%d",&x);
  61. int flag=0;
  62. for(k=0; k<m; k++)
  63. {
  64. int t=x-c[k];
  65. if(BinarySearch(ab,h,t))
  66. {
  67. printf("YES\n");
  68. flag=1;
  69. break;
  70. }
  71. }
  72. if(!flag) printf("NO\n");
  73. }
  74. }
  75. return 0;
  76. }

Can you find it? HDU - 2141 (二分查找)的更多相关文章

  1. Can you find it?(hdu 2141 二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  2. Equations(hdu 1496 二分查找+各种剪枝)

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. Pie(hdu 1969 二分查找)

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  4. hdu 2141 Can you find it?(二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...

  5. hdu 2141:Can you find it?(数据结构,二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  6. hdu 2141 Can you find it?(二分查找变例)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  7. HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

  8. HDU 2141 Can you find it? (二分)

    题目链接: Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/ ...

  9. 二分查找 HDOJ 2141 Can you find it?

    题目传送门 /* 题意:给出一个数,问是否有ai + bj + ck == x 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x */ ...

随机推荐

  1. [洛谷P2750] [USACO5.5]贰五语言Two Five

    洛谷题目链接:[USACO5.5]贰五语言Two Five 题目描述 有一种奇怪的语言叫做"贰五语言".它的每个单词都由A-Y这25个字母各一个组成.但是,并不是任何一种排列都是一 ...

  2. PHP与数据库

    连接数据库 Connect 访问数据库的数据之前,先要与数据库建立连接,使用mysql_connect()方法与数据库建立连接. mysql_connect()参数 <?php //server ...

  3. Handlerbars基础笔记

    此笔记摘抄于杨元的博客(http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) 引入 <script type=&qu ...

  4. [php]数组建立方式

    1.$a[0]=..; $a[1]=..; $a[2]=..; $a[3]=..; 2.$a=array(1,2,3,4,5); 3.自定义数组 $a['logo']="qq"; ...

  5. 基本控件文档-UIView属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  6. 【洛谷 P1772】 [ZJOI2006]物流运输(Spfa,dp)

    题目链接 \(g[i][j]\)表示不走在\(i\text{~}j\)时间段中会关闭的港口(哪怕只关\(1\)天)从\(1\)到\(m\)的最短路. \(f[i]\)表示前\(i\)天的最小花费.于是 ...

  7. 大聊Python----装饰器

    什么是装饰器? 装饰器其实和函数没啥区别,都是用def去定义的,其本质就是函数,而功能就是装饰其他的函数,说白了就是为其他函数提供附加功能 装饰器有什么作用? 比如你是一个公司的员工,你所写的程序里有 ...

  8. 蓝色的PC端后台管理界面设计模板——后台

    链接:http://pan.baidu.com/s/1o82hXX4 密码:x6le

  9. ckeditor+ckfinder+java

    (java)Ckdeitor+ckfinder整合 第一步:工具下载 首先下载:CKEditor 地址:http://cdeditor.com/dowmload 接着下载CKFinder 地址:htt ...

  10. HTML表单属性与全局属性

    1.全局属性