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,和确定的数x,问是否存在i,j使满足A[i]+B[j]=x的?最快的方法是枚举A,然后在B中二分查找

x-A。现在回到这个问题,这道题给了三组序列A,B,C如何查找呢?我们不妨将A,C两数组合并成新数组LN,LN中每个元素都是Ai+Bj的和。然后枚举B,在LN中二分查找x-B。

*这里有个细节:由于x为32位整数,当A+B>INT32_MAX时,可以不用加入LN.

代码如下:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <time.h>
using namespace std;
#define clock__ printf("%f\n",double(clock())/CLOCKS_PER_SEC);
#define maxn 500
#define INT_32_MAX ((1<<31)-1)
typedef long long LL;
int A[maxn+],B[maxn+],C[maxn+];
int L,M,N,S;
int LN[maxn*maxn+];
int h; bool search_LN(int a,int b,int x){
int len=b-a;
int mid=a+len/;
if(len==){
if(LN[a]==x) return true;
else return false;
}
if(LN[mid]==x) return true;
else if(LN[mid]>x) return search_LN(a, mid, x);
else return search_LN(mid, b, x);
} int main() { int T=;
while(scanf("%d%d%d",&L,&M,&N)==){
printf("Case %d:\n",++T);
for(int i=;i<L;i++)
scanf("%d",&A[i]);
for(int i=;i<M;i++)
scanf("%d",&B[i]);
for(int i=;i<N;i++)
scanf("%d",&C[i]);
h=;
for(int i=;i<L;i++)
for(int j=;j<N;j++){
if(LL(A[i])+C[j]<=INT_32_MAX)
LN[h++]=A[i]+C[j];
}
sort(LN, LN+h);
scanf("%d",&S);
for(int i=;i<=S;i++){
int x;
scanf("%d",&x);
bool ok=;
for(int j=;!ok&&j<M;j++){
if(search_LN(,h, x-B[j]))
ok=;
}
if(ok)printf("YES\n");
else printf("NO\n");
}
}
//clock__
return ;
}

Can you find it?——[二分查找]的更多相关文章

  1. River Hopscotch-[二分查找、贪心]

    Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...

  2. 714 - Copying Books——[贪心、二分查找]

    Before the invention of book-printing, it was very hard to make a copy of a book. All the contents h ...

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

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

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

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

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

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

  6. java实现二分查找

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

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

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

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

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

  9. 【Python】二分查找算法

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

随机推荐

  1. PHP小知识总结(1)

    1. mysqli_query — 对数据库执行一次查询 失败时返回 FALSE ,通过 mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回 ...

  2. 转载 初探Promise

    初探Promise https://segmentfault.com/a/1190000007032448 javascript es6 promise 33.5k 次阅读  ·  读完需要 65 分 ...

  3. windows.open window.location.href的用法和区别

    window.location.href  只能在当前页面打开,不能用新窗口打开 windows.open("URL","窗口名称","窗口外观设定& ...

  4. HDU 4193

    本题思路:用sum[]数组维护前缀和, 当然这里需要把原数组扩大为原来的两倍. 然后对于任意一个长度为n的区间 k.....k+n-1,如果有该区间内的最小值大于等于sum[k-1]那么该种情况就符合 ...

  5. scrapy 调用js

    依赖: PyExecJS 使用案例: from execjs import execjs text = response.xpath('//script/text()')[1].get() ctx = ...

  6. 【JZOJ4886】【NOIP2016提高A组集训第13场11.11】字符串

    题目描述 某日mhy12345在教同学们写helloworld,要求同学们用程序输出一个给定长度的字符串,然而发现有些人输出了一些"危险"的东西,所以mhy12345想知道对于任意 ...

  7. Linux 下的python操作redis

    python操作redis   Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).se ...

  8. hdu 1561【树形dp+01背包】

    http://acm.hdu.edu.cn/showproblem.php?pid=1561 很容易想到如果是要攻克v城需要先攻克u城的话,可以建u到v的边.但是如果能够直接攻克u城呢?无边可建,这样 ...

  9. WPF疑难杂症之一(3D场景)

    原文:WPF疑难杂症之一(3D场景) 最近2个月一直在学习WPF,在实际的开发中遇到下面一个3D场景有关的问题,我先给出问题代码:首先是在资源中定义了一个3D变换组:<Window x:Clas ...

  10. 关于6410板文件的dm9000的平台设备地址

    转自csdn #define CONFIG_DM9000_BASE 0x20000300#define DM9000_IO                      0x20000000#define ...