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. 外贸电子商务网站之Prestashop修改顶部导航

    如修改以上所示顶部导航. 如何在prestashop顶部导航栏添加链接,Module>Top horizontal menu点击进入Configure页面 1,在Settings 中看到 链接 ...

  2. 【SPOJ 220】 PHRASES - Relevant Phrases of Annihilation

    [链接]h在这里写链接 [题意]     给你n(n<=10)个字符串.     每个字符串长度最大为1e4;     问你能不能找到一个子串.     使得这个子串,在每个字符串里面都不想交出 ...

  3. 数据挖掘算法R语言实现之决策树

    数据挖掘算法R语言实现之决策树 最近,看到很多朋友问我如何用数据挖掘算法R语言实现之决策树,想要了解这方面的内容如下: > library("party")导入数据包 > ...

  4. 【Django入坑之路】Django后台上传图片,以及前端的显示

    #setting配置: MEDIA_URL = "/media/" MEDIA_ROOT = os.path.join(BASE_DIR, "media") # ...

  5. 【OI】倍增求LCA

    ╭(′▽`)╯ 总之,我们都知道lca是啥,不需要任何基础也能想出来怎么用最暴力的方法求LCA,也就是深度深的点先跳到深度浅的点的同一深度,然后一起向上一步步跳.这样显然太慢了! 所以我们要用倍增,倍 ...

  6. openstack安装dashboard后访问horizon出错 500 or 504

    访问controller/horizon出错500:internal server error 访问controller/horizon出错504:internal server error  gat ...

  7. 【JZOJ1922】【Usaco 2005 NOV Gold】小行星群

    题目描述 Bessie想驾驶她的飞船穿过危险的小行星群,小行星群是一个N×N的网格(1 <= N <= 500),在网格内有K个小行星(1 <= K <= 10,000). 幸 ...

  8. MaxCompute 费用暴涨之存储压缩率降低导致SQL输入量变大

    现象:同样的SQL,每天处理的数据行数差不多,但是费用突然暴涨甚至会翻数倍. 分析: 我们先明确MaxCompute SQL后付费的计费公式:一条SQL执行的费用=扫描输入量 ️ SQL复杂度 ️ 0 ...

  9. some daily

    1. 一般div元素的background-color只覆盖到border,而其margin的颜色由外层元素的背景色决定. 2. 当设置了border-box以后,width=border+paddi ...

  10. 域名拆分 tld

    概念 URL Universal Resource Locator ,统一资源定位符. 用处:用来标识互联网资源的唯一地址. 本质:提供了互联网上任一资源地址的通用表示方法. protocol://h ...