O - Can you find it?

Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

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

//很简单的题目

运用二分的思想去查找 a 和 b 组合的数组,是否有等于要求的

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. using namespace std;
  5.  
  6. int a[];
  7. int b[];
  8. int c[];
  9. int sum[*];
  10. int x,y,z,num;//数组a,b,c的元素个数
  11.  
  12. int find(int x)
  13. {
  14. int l=,r=num-;
  15. int mid;
  16. while (l<=r)
  17. {
  18. mid=(l+r)/;
  19. if (sum[mid]==x) return ;
  20. if (sum[mid]>x) r=mid-;
  21. if (sum[mid]<x) l=mid+;
  22. }
  23. return ;
  24. }
  25.  
  26. int main()
  27. {
  28. int Case=;
  29. int i,j;
  30. while (scanf("%d%d%d",&x,&y,&z)!=EOF)
  31. {
  32. for(i=;i<x;i++)
  33. scanf("%d",&a[i]);
  34. for(i=;i<y;i++)
  35. scanf("%d",&b[i]);
  36. for(i=;i<z;i++)
  37. scanf("%d",&c[i]);
  38. sort(c,c+z);
  39. num=;
  40. for (i=;i<x;i++)
  41. {
  42. for (j=;j<y;j++)
  43. {
  44. sum[num++]=a[i]+b[j];
  45. }
  46. }
  47. sort(sum,sum+num);
  48. int t,times;
  49. scanf("%d",&times);
  50. printf("Case %d:\n",++Case);
  51. while (times--)
  52. {
  53. scanf("%d",&t);
  54. for (i=;i<z;i++)
  55. {
  56. if (find(t-c[i]))
  57. {
  58. printf("YES\n");
  59. break;
  60. }
  61. }
  62. if (i==z)
  63. printf("NO\n");
  64. }
  65. }
  66. return ;
  67. }

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

  1. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  2. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  3. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  4. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  5. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

  6. c#-二分查找-算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...

  7. 【Python】二分查找算法

    二分查找:在一段数字内,找到中间值,判断要找的值和中间值大小的比较.如果中间值大一些,则在中间值的左侧区域继续按照上述方式查找.如果中间值小一些,则在中间值的右侧区域继续按照上述方式查找.直到找到我们 ...

  8. PHP实现文本快速查找 - 二分查找

    PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...

  9. java二分查找举例讨论

    最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...

  10. JAVA源码走读(二)二分查找与Arrays类

    给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序.比较数组:通过equals方法比较数组中元素值是否相等.查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找 ...

随机推荐

  1. C#发起Http请求,调用接口

    //方法1. Post 异步请求,普通的异步请求,传输普通的字符串等,对于有html代码的字段值的传输支持不好,如果需要传输html,二进制等数据的传输,请使用下面第二个方法,即使用UploadDat ...

  2. MSYS2 更新源

    博客转载自:https://blog.csdn.net/puputaoexin/article/details/81780492 在使用msys2下载文件的时候回出现下载速度奇慢,且经常因为各种原因报 ...

  3. libpng error: IHDR: CRC error

    原本正常显示在主页端logo图片无法显示了,爆出如下错误: libpng error: IHDR: CRC error 查找原因如下:

  4. Qt开发经验小技巧21-30

    如果出现Z-order assignment: is not a valid widget.错误提示,用记事本打开对应的ui文件,找到为空的地方,删除即可. 善于利用QComboBox的addItem ...

  5. 安卓 android studio 报错 The specified Android SDK Build Tools version (27.0.3) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle

    今天将项目迁移到另一台笔记本,进行build出现以下问题,导致build失败 报错截图: 大致意思,目前使用的build工具版本27.0.3不合适.因为当前使用Gradle插件版本是3.2.1,这个版 ...

  6. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  7. Python3 列表list合并的4种方法

    方法1: 直接使用"+"号合并列表 aList = [1,2,3] bList = ['www', 'pythontab.com'] cList = aList + bList   ...

  8. java的byte[]与String相互转换

    String转byte[] byte[] sInput = new byte[0]; try { // 可以指定编码,默认也只UTF-8 sInput = "这是内容".getBy ...

  9. Qt去掉treeview项的焦点虚线

    项目做到后期,进行局部美化的时候发现了问题,在treeview框选择状态下会有虚线. 其实,不仅是treeview,tableview,listview,乃至button在有焦点的情况下,都会出现虚线 ...

  10. Spring中的AOP实现思路

    AOP是面向切面编程,为什么在切面中写一个注解方法@Before,这个方法会在目标方法前面执行呢 基于JDK动态代理实现上面说的情况 自定义注解 @Target({ ElementType.METHO ...