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数列中查找。
  没想到这样的方法,脑子还是太木,不甘心啊 >_<
  本题代码:
 
 #include <iostream>
#include <algorithm>
using namespace std;
int qn; //sab数组总数
int sab[];
int binsearch(int q[],int n,int k) //二分查找
{
int left=,right=n,mid;
while(left<=right){
mid = (left+right)/;
if(q[mid]==k)
return mid;
if(q[mid]>k)
right = mid - ;
else
left = mid + ;
}
return ;
}
int main()
{
int l,n,m;
int count = ;
while(cin>>l>>n>>m){
qn = ;
int A[],B[],C[];
for(int i=;i<=l;i++){
cin>>A[i];
}
for(int i=;i<=n;i++){
cin>>B[i];
}
for(int i=;i<=m;i++){
cin>>C[i];
}
for(int i=;i<=l;i++)
for(int j=;j<=n;j++)
sab[qn++] = A[i] + B[j]; //产生sab数列
sort(sab+,sab+qn-); //对sab数列进行排序
int s;
cin>>s;
cout<<"Case "<<count++<<":"<<endl;
while(s--){
int t;
cin>>t;
int i;
for(i=;i<=m;i++){
int tt = t - C[i];
if(binsearch(sab,qn-,tt)){ //查找有没有 x-c
cout<<"YES"<<endl;
break;
}
}
if(i>m)
cout<<"NO"<<endl;
}
}
return ;
}

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. lucene 学习一

    索引工具的三部分 1.索引部分 2.分词部分 3.搜索部分 查看索引的工具:luke   java -jar fileName.jar 目标:为文件夹的所有的文档生成索引并搜索它 package co ...

  2. Android系统源代码——所需工具

    一.概述 众所周知,Android移动操作系统是Google花费了很大的财力.物力及人力的前提下,推广到世界各个角落,以开放源代码的方式(当然也不是完全开放所有),使其在世界范围内迅速漫延开来,到目前 ...

  3. db.students.batchInsert is not a function :@(shell):1:1

    按照<mongdb权威指南>当使用version 3.4.1版本的mongodb,其中使用batchInsert函数进行对students集合进行批量插入 db.students.batc ...

  4. Directshow开发播放器相关介绍

    原文地址:http://www.cnblogs.com/qiufa/archive/2006/12/19/596949.html DirectShow技术是DirectX推出的建立在DirectDra ...

  5. C++的引用与const指针的关系以及各种传递方式

    首先我们知道 const int *p 与 int const *p 是一样的,即 *p 是常量:而 int * const p 跟上面是不一样的,即 p 是常量:我们知道引用只是一个别名,与变量共享 ...

  6. unity3D中使用Socket进行数据通信(二)

    上一篇博客主要介绍了使用socket搭建服务端和client程序,这一篇来说说socket的数据传输,我们使用socket的目的是解决点对点之间的数据传输,之前提到了socket中一个重要的概念:po ...

  7. cocos2dx 3.3 异步加载纹理

    这里以3d场景加载为例,2d情况类似. 先同步加载模型数据和尺寸缩小了100倍的贴图,创建mesh.然后异步加载所有精细纹理并每加载完一个就替换一个,并进入场景. 如此做法的效果是当刚进入场景时看到的 ...

  8. 使用Secure Boot后,导致VMware无法启动虚拟机

    最初安装vmware时就报错  Gtk-Message: Failed to load module "canberra-gtk-module": libcanberra-gtk- ...

  9. 穷人的语义处理工具箱之中的一个:语义版Jaccard

    /* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 |为什么我们是ML界的穷人 假设对工业界里的机器学习(ML)从业者进行阶级划分的话,划线标准不是你用的 ...

  10. 源码分析HotSpot GC过程(二):DefNewGeneration的GC过程

    由于虚拟机的分代实现,虚拟机不会考虑各个内存代如何实现垃圾回收,具体的工作(对象内存的分配也是一样)由各内存代根据垃圾回收策略自行实现. DefNewGeneration的使用复制算法进行回收.复制算 ...