Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 9180    Accepted Submission(s): 2401

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
 
Author
wangye
 
Source
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2199 2899 2289 1597 1551 

 
  数据结构:二分(折半)查找。
  这道题一开始还在纳闷怎么用二分查找,后来看人家的思路才发现需要用巧办法,做法是先将前两个数列相加产生sab数列,这个时候sab+c = x,那么sab= x-c,每次询问x时,就用x减去c中的所有数,依次在sab数列中查找。
  没想到这样的方法,脑子还是太木,不甘心啊 >_<
  本题代码:
 
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int qn; //sab数组总数
  5. int sab[];
  6. int binsearch(int q[],int n,int k) //二分查找
  7. {
  8. int left=,right=n,mid;
  9. while(left<=right){
  10. mid = (left+right)/;
  11. if(q[mid]==k)
  12. return mid;
  13. if(q[mid]>k)
  14. right = mid - ;
  15. else
  16. left = mid + ;
  17. }
  18. return ;
  19. }
  20. int main()
  21. {
  22. int l,n,m;
  23. int count = ;
  24. while(cin>>l>>n>>m){
  25. qn = ;
  26. int A[],B[],C[];
  27. for(int i=;i<=l;i++){
  28. cin>>A[i];
  29. }
  30. for(int i=;i<=n;i++){
  31. cin>>B[i];
  32. }
  33. for(int i=;i<=m;i++){
  34. cin>>C[i];
  35. }
  36. for(int i=;i<=l;i++)
  37. for(int j=;j<=n;j++)
  38. sab[qn++] = A[i] + B[j]; //产生sab数列
  39. sort(sab+,sab+qn-); //对sab数列进行排序
  40. int s;
  41. cin>>s;
  42. cout<<"Case "<<count++<<":"<<endl;
  43. while(s--){
  44. int t;
  45. cin>>t;
  46. int i;
  47. for(i=;i<=m;i++){
  48. int tt = t - C[i];
  49. if(binsearch(sab,qn-,tt)){ //查找有没有 x-c
  50. cout<<"YES"<<endl;
  51. break;
  52. }
  53. }
  54. if(i>m)
  55. cout<<"NO"<<endl;
  56. }
  57. }
  58. return ;
  59. }

Freecode : www.cnblogs.com/yym2013

hdu 2141:Can you find it?(数据结构,二分查找)的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

  4. Go 数据结构--二分查找树

    Go 数据结构--二分查找树 今天开始一个Go实现常见数据结构的系列吧.有时间会更新其他数据结构. 一些概念 二叉树:二叉树是每个节点最多有两个子树的树结构. 完全二叉树:若设二叉树的高度为h,除第 ...

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

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

  6. hdoj 2141 Can you find it?【二分查找+暴力】

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

  7. HDU 5878 I Count Two Three (预处理+二分查找)

    题意:给出一个整数nnn, 找出一个大于等于nnn的最小整数mmm, 使得mmm可以表示为2a3b5c7d2^a3^b5^c7^d2​a​​3​b​​5​c​​7​d​​. 析:预处理出所有形为2a3 ...

  8. 题解报告: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 ...

  9. HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...

  10. 数据结构-二分查找(Binary Search)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

随机推荐

  1. chrome打包程序

      使用chrome如何打包扩展程序中已经存在的插件及所遇到的问题 CreateTime--2017年7月4日07:41:33 Author:Marydon 一.前言 鉴于本文章的访问量大,特此进行多 ...

  2. 查看 apache、nginx、php、mysql 的编译参数

    查看nginx编译参数:/usr/local/nginx/sbin/nginx -V 查看apache编译参数:cat /usr/local/apache2/build/config.nice 查看m ...

  3. 开源爬虫Labin,Nutch,Neritrix介绍和对比

    crawler 开发 语言 功能 单一 支持分布式 爬取 效率 镜像 保存 Nutch Java × √ 低 × Larbin C++ √ × 高 √ Heritrix Java √ × 中 √ ** ...

  4. Android API之android.view.View.MeasureSpec

    android.view.View.MeasureSpec MeasureSpec是View的内部类 public static class MeasureSpec MeasureSpec封装从par ...

  5. cxf 生成客户端代码调用服务

    cxf是另一种发布webservice的方式,与jdk提供的相比 jdk提供的是wsimport cxf 提供的是 wsdl2java- d 地址 根据http://www.cnblogs.com/f ...

  6. Linux命令-压缩解压命令:zip、unzip

    zip [选项] [压缩后文件名] [压缩前的文件或者目录名称] -r表示压缩目录(recursion 递归) rm -rf * 删除当前目录下面的所有文件,也包括目录和子目录ls cp /etc/s ...

  7. 一个Keygen,参考参考

    看到一个Keygen,我觉得还可以,参考参考 //////////////////////////////////////////////////////////////////// //// key ...

  8. 使用 hibernate validator 进行表单验证

    1 引入依赖,如果是 Maven 项目,仅需要添加如下依赖.官网请查看http://hibernate.org/validator/documentation/getting-started/ < ...

  9. JVM虚拟机(三):参数配置

    在虚拟机运行的过程中,如果可以跟踪系统的运行状态,那么对于问题的故障排查会有一定的帮助,为此,虚拟机提供了一些跟踪系统状态的参数,使用给顶的参数执行java虚拟机,就可以在系统运行时打印相关日志,用于 ...

  10. JAVA数字特征值

    数字特征值(5分) 题目内容: 对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值.对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推.这个整数在第n位上的数字记作x, ...