Problem 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
 

初看很简单就是判断三个数加起来的和,但是三个for循环速度还是很慢的,所以细想要用到二分查找。

把函数改为:A+B=X-C,然后二分搜一下就可以了。

完全用的是二分查找的模板。

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <queue>
  4. #include <vector>
  5. #include <stack>
  6. #include <map>
  7. #include <string>
  8. #include <string.h>
  9. #include <algorithm>
  10. #include <iostream>
  11. using namespace std;
  12. #define K 505
  13. int LN[K*K];
  14. int binarysearch(int a[],int l,int r,int k){
  15. int mid;
  16. while(r-l>){
  17. mid=(r+l)/;
  18. if(a[mid]<=k)
  19. l=mid;
  20. else
  21. r=mid;
  22. }
  23. if(a[l]==k)
  24. return ;
  25. else
  26. return ;
  27. }//二分查找
  28. int main()
  29. {
  30. int i,j,count=,q;
  31. int L[K],N[K],M[K],s,n,m,l;
  32. while(~scanf("%d%d%d",&l,&n,&m)){
  33. int h=;
  34. for(i=;i<l;i++)
  35. scanf("%d",&L[i]);
  36. for(i=;i<n;i++)
  37. scanf("%d",&N[i]);
  38. for(i=;i<m;i++)
  39. scanf("%d",&M[i]);
  40. for(i=;i<l;i++)
  41. for(j=;j<n;j++)
  42. LN[h++]=L[i]+N[j];
  43. sort(LN,LN+h);
  44. scanf("%d",&s);
  45. printf("Case %d:\n",count++);
  46. for(i=;i<s;i++)
  47. {
  48. scanf("%d",&q);
  49. int p=;
  50. for(j=;j<m;j++)
  51. {
  52. int a=q-M[j];
  53. if(binarysearch(LN,,h,a))
  54. {
  55. printf("YES\n");
  56. p=;
  57. break;
  58. }
  59. }
  60. if(!p)
  61. printf("NO\n");
  62. }
  63. }
  64. return ;
  65. }

hdu 2141 Can you find it?(二分查找变例)的更多相关文章

  1. C - 啥~ 渣渣也想找玩数字 HDU - 2141(有序序列枚举 + 二分优化查找)

    题目描述 可爱的演演又来了,这次他想问渣渣一题... 如果给你三个数列 A[],B[],C[],请问对于给定的数字 X,能否从这三个数列中各选一个,使得A[i]+B[j]+C[k]=X? 输入 多组数 ...

  2. hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/1000 ...

  3. HDU 2141 Can you find it? [二分]

    Can you find it? Give you three sequences of numbers A, B, C, then we give you a number X. Now you n ...

  4. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

  5. HDU 5265 pog loves szh II (二分查找)

    [题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...

  6. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

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

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

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

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

  9. Can you find it? HDU - 2141 (二分查找)

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

随机推荐

  1. Java 将字节数组转化为16进制的多种方案

    很多时候我们需要将字节数组转化为16进制字符串来保存,尤其在很多加密的场景中,例如保存密钥等.因为字节数组,除了写入文件或者以二进制的形式写入数据库以外,无法直接转为为字符串,因为字符串结尾有\0,当 ...

  2. mysql语句在node.js中的写法

    总结一下mysql语句在node.js中的各种写法,参考了npm网站mysql模块给的实例. 查询 select //1 db.query('select * from tuanshang_users ...

  3. 【C语言探索之旅】 第二部分第六课:创建你自己的变量类型

    内容简介 1.课程大纲 2.第二部分第六课: 创建你自己的变量类型 3.第二部分第七课预告:   文件读写 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C ...

  4. win7开机直接进入系统系统桌面

    在开始搜索栏输入:netplwiz   选中admin用户(也就是你登录的用户名),然后在把上面的勾去掉,点应用,会提示输入用户名和密码,输二次,是一样的密码,如果没设密码,就不用输直接应用,确定就O ...

  5. php+sqlite 最佳web服务器

    1 wampserver   支持mysql.每次都启动mysql,可以手动停止.但是运行时有时会很慢. 放弃 2 APS绿色版(Apache+PHP+SQLite)  组件环境:Apache2.2. ...

  6. 【Android进阶】判断网络连接状态并自动界面跳转

    用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面 /** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protecte ...

  7. ssh远程登录报错REMOTE HOST IDENTIFICATION HAS CHANGED!解决方式及原因

    注意,文档中的ip和指纹已经替换为了ip.ip.ip.ip 和aa:... ,以免引起不必要的误会. icode@test:~/lab/dir/sadf$ ssh remote_name@ip.ip. ...

  8. php中国的垃圾问题

    header这条线加,这是解决中国乱码的问题. 版权声明:本文博主原创文章,博客,未经同意不得转载.

  9. 第十二章——SQLServer统计信息(3)——发现过期统计信息并处理

    原文:第十二章--SQLServer统计信息(3)--发现过期统计信息并处理 前言: 统计信息是关于谓词中的数据分布的主要信息源,如果不知道具体的数据分布,优化器不能获得预估的数据集,从而不能统计需要 ...

  10. T-SQL开发——ID处理篇

    原文:T-SQL开发--ID处理篇 数据库自增ID功能中Identity.Timestamp.Uniqueidentifier的区别: 问题现象: 一般序号的产生,对于一般程序员而言,都是使用T-SQ ...