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. centos6.10环境安装nodejs8.2.1

    操作系统为centos6.10,在安装nodejs最新版本的时候报错,依赖glibc的高版本和gcc高版本,还要安装python2.7,操作系统上已经跑了很多应用,升级gcc风险过大,采用相对保守的方 ...

  2. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  3. 【mysql 默认密码】ubuntu 上 初次启动mysql 默认密码

    对于debian系的系统,mysql初始默认密码 cat /etc/mysql/debian.cnf

  4. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  5. Eclipse安装Properties Editor插件

    安装步骤 1.打开eclispe编辑器help-->install new soft 2.输入软件地址 name:properties editor Location:http://proped ...

  6. 【视频开发】RTSP SERVER(基于live555)详细设计

    /* *本文基于LIVE555的嵌入式的RTSP流媒体服务器一个设计文档,个中细节现剖于此,有需者可参考指正,同时也方便后期自己查阅.(本版本是基于2011年的live555) 作者:llf_17@q ...

  7. idea中默认maven配置

    使用idea时,通常会配置自己的maven,但是发现每创建一个新项目都需要重新配置一遍,原来在idea中有一个默认配置,修改后就会全局生效,具体配置如下: 简单两步,配置完成.

  8. 解决ScrollView嵌套百度地图滑动冲突

    一.问题描述 scrollview中嵌套百度地图时会出现滑动冲突,地图无法滑动的情况. 二.期望结果 焦点在地图上时,只有地图移动,焦点在地图外部时,可以滑动scrollview. 三.解决方法 自定 ...

  9. 各种实用的 PHP 开源库推荐【转】

    转自: https://my.oschina.net/editorial-story/blog/882780 PHP 是一种通用开源脚本语言.语法吸收了 C 语言.Java 和 Perl 的特点,利于 ...

  10. TCP粘包和拆包的定义,产生的原因以及解决方案

    TCP粘包:指发送方发送的若干数据包在接收方接收时粘成一团,从接收缓冲区看,后一包数据的头紧接着前一包数据的尾 产生的原因: 1.发送方的原因:TCP默认使用Nagle算法,而Nagle算法主要做两件 ...