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. ubuntu上安装notepadpp

    Notepad++是一套非常有特色的自由软件的纯文字编辑器(许可证:GPL).有完整的中文化接口及支持多国语言编写的功能(UTF8 技术).它的功能比 Windows 中的 Notepad(记事本)强 ...

  2. 弘康人寿基于 RocketMQ 构建微服务边界总线的实践

    随着互联网+和平台化战略的兴起,各个行业的 IT 系统都在向互联网架构发展,涉及的主要技术包括微服务.消息和弹性计算等,采用微服务架构实现服务高内聚.低耦合,通过异步消息完成交易快速响应和高并发.由于 ...

  3. Significance A and B for protein ratios

    实验设计中,一般会做三个生物学重复来确保结果的准确性,尤其在下游分析中.但有时会遇到没有生物学重复,而又需要进行差异分析的情况,这时一般建议考虑foldchange即可,因为根本无法进行T-test等 ...

  4. nodeJs学习-19 个人博客案例-(1)数据字典

    智能社视频27.28 数据字典: 定义: url 300字 admin_table 管理员用户表 ID username varchar(32) password varchar(32) banner ...

  5. nodeJs学习-17 博客案例

    源码:智能社视频20节课件 const express=require('express'); const static=require('express-static'); const cookie ...

  6. utf8mb4 使用注意

    数据库的表的定义如果是utf8mb4的富文本时,关联的字段必须指定为非utf8,否则 跟其他的表关联的时候,会非常慢,以至于索引都不能使用. 也就是必须的字段才可以使用这个 utf8mb4 ,否则检索 ...

  7. hdu4267 线段树

    开始敲了一发线段树,觉得可以暴力一点的过,tle了.后来进行修改,发现了问题. 后来一看大神的做法,由于1<=k<=10,所以对于不同的k,有55个余,找答案的时候只要找不同的k值满足条件 ...

  8. 【OI】拓扑排序

    拓扑排序 首先要求图为DAG 算法:首先将度为1的节点加入队列每次取出队首点u,在图中删去和u相邻的边继续将度数为1的点加入队列 到了最后, 如果没有度数为1的点,则图不是DAG 通过拓扑排序可以给D ...

  9. Android Studio 如何引入.jar文件和.so文件?

    最近刚从Eclipse投入Android Studio的怀抱,可是在开发一个地图有关的应用,添加高德地图API的.jar和.so库文件时,遇到了问题.在Eclipse中只要简单地复制粘贴就可以了,但是 ...

  10. C++之MD5加密(签名)

    md5.h : #include <stdio.h> #include <stdlib.h>#include <time.h> #include <strin ...