O - Can you find it?

Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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 组合的数组,是否有等于要求的

 #include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std; int a[];
int b[];
int c[];
int sum[*];
int x,y,z,num;//数组a,b,c的元素个数 int find(int x)
{
int l=,r=num-;
int mid;
while (l<=r)
{
mid=(l+r)/;
if (sum[mid]==x) return ;
if (sum[mid]>x) r=mid-;
if (sum[mid]<x) l=mid+;
}
return ;
} int main()
{
int Case=;
int i,j;
while (scanf("%d%d%d",&x,&y,&z)!=EOF)
{
for(i=;i<x;i++)
scanf("%d",&a[i]);
for(i=;i<y;i++)
scanf("%d",&b[i]);
for(i=;i<z;i++)
scanf("%d",&c[i]);
sort(c,c+z);
num=;
for (i=;i<x;i++)
{
for (j=;j<y;j++)
{
sum[num++]=a[i]+b[j];
}
}
sort(sum,sum+num);
int t,times;
scanf("%d",&times);
printf("Case %d:\n",++Case);
while (times--)
{
scanf("%d",&t);
for (i=;i<z;i++)
{
if (find(t-c[i]))
{
printf("YES\n");
break;
}
}
if (i==z)
printf("NO\n");
}
}
return ;
}

O - Can you find it?(二分查找)的更多相关文章

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

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

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

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

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

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

  4. java实现二分查找

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

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

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

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

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

  7. 【Python】二分查找算法

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

  8. PHP实现文本快速查找 - 二分查找

    PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...

  9. java二分查找举例讨论

    最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...

  10. JAVA源码走读(二)二分查找与Arrays类

    给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序.比较数组:通过equals方法比较数组中元素值是否相等.查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找 ...

随机推荐

  1. 如何查看window 7/window 8 等系统 的激活状态?

    http://www.officezhushou.com/office-key/   Office激活密钥 Win+R 输入: slmgr.vbs -dlv 显示:最为详尽的激活信息,包括:激活ID. ...

  2. window git安装 以及 tortoiseGit安装与使用

    一:Git安装 使用TortoiseGit这个程序,需要先安装Git Windows版本Git的下载页面: http://git-scm.com/download/win 1:下载Git到文件夹, 2 ...

  3. [LeetCode] 548. Split Array with Equal Sum 分割数组成和相同的子数组

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

  4. WCF之Windows宿主

    WCF之Windows宿主(可安装成服务自动并启动) WCF之Windows宿主(可安装成服务自动并启动) 创建解决方案WCFServiceDemo 创建WCF服务库(类库或WCF服务库)WCFSer ...

  5. Office系列常用快捷键

    office三件套,常用的快捷键. Word常用快捷键 查找文字.格式和特殊项 Ctrl+G 使字符变为粗体 Ctrl+B 为字符添加下划线 Ctrl+U 删除段落格式 Ctrl+Q 复制所选文本或对 ...

  6. Oracle VM VirtualBox安装配置虚拟机Redhat7.6

    首先,准备好材料,需要下载Oracle VM VirtualBox.Oracle19C的安装包.Redhat7.6镜像 下面列出地址: Oracle VM VirtualBox安装包:链接:https ...

  7. mysql网文收录

    1.分布式事务 1)  聊聊分布式事务,再说说解决方案 https://www.cnblogs.com/savorboard/p/distributed-system-transaction-cons ...

  8. [LOJ2290] [THUWC2017] 随机二分图

    题目链接 LOJ:https://loj.ac/problem/2290 洛谷:https://www.luogu.org/problemnew/show/P4547 Solution 首先考虑只有第 ...

  9. [LOJ2537] [PKUWC2018] Minimax

    题目链接 LOJ:https://loj.ac/problem/2537 洛谷:https://www.luogu.org/problemnew/show/P5298 Solution 不定期诈尸 好 ...

  10. 牛客挑战赛33 B-鸽天的放鸽序列

    也许更好的阅读体验 \(\mathcal{Description}\) 定义一个长为\(n\)的\(01\)序列\(A_1, A_2, \dots, A_n\)​的权值为\(\sum_{i=1}^n ...