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

//该题的思想是先合并前两组,然后用题目中的x减去第3组的值
//然后在合并组里面二分查找,看是否能找到一个值与x减去第三组的值相等
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const int INF=1e9+7;
const int maxn=510;
typedef long long ll; int l,n,m,S;
int a[maxn],b[maxn],c[maxn],ab[maxn*maxn]; int BinarySearch(int ab[],int h,int t)//二分查找
{
int left=0;
int right=h-1;
int mid=(left+right)/2;
while(left<=right)
{
mid=(left+right)/2;
if(ab[mid]==t)
return 1;
else if(ab[mid]>t)
right=mid-1;
else if(ab[mid]<t)
left=mid+1;
}
return 0;
} int main()
{
int cot=1;
int i,j,k,h,x;
while(scanf("%d %d %d",&l,&n,&m)!=EOF)
{
h=0;
for(i=0; i<l; i++)
scanf("%d",&a[i]);
for(j=0; j<n; j++)
scanf("%d",&b[j]);
for(k=0; k<m; k++)
scanf("%d",&c[k]);
for(i=0; i<l; i++)
for(j=0; j<n; j++)
ab[h++]=a[i]+b[j];
sort(ab,ab+h);
printf("Case %d:\n",cot++);
scanf("%d",&S);
for(int s=0; s<S; s++)
{
scanf("%d",&x);
int flag=0;
for(k=0; k<m; k++)
{
int t=x-c[k];
if(BinarySearch(ab,h,t))
{
printf("YES\n");
flag=1;
break;
}
}
if(!flag) printf("NO\n");
}
}
return 0;
}

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

  1. Can you find it?(hdu 2141 二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  2. Equations(hdu 1496 二分查找+各种剪枝)

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. Pie(hdu 1969 二分查找)

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  4. hdu 2141 Can you find it?(二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...

  5. hdu 2141:Can you find it?(数据结构,二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  6. hdu 2141 Can you find it?(二分查找变例)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  7. HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

  8. HDU 2141 Can you find it? (二分)

    题目链接: Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/ ...

  9. 二分查找 HDOJ 2141 Can you find it?

    题目传送门 /* 题意:给出一个数,问是否有ai + bj + ck == x 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x */ ...

随机推荐

  1. (function($){})(jQuery)---Javascript的神级特性:闭包

    function($){}实际上是匿名函数 这就定义了一个匿名函数,参数为arg function(arg){ //code } 而调用函数 时,是在函数后面写上括号和实参的,由于操作符的优先级,函数 ...

  2. 浅谈Stein算法求最大公约数(GCD)的原理及简单应用

    一.Stein算法过程及其简单证明 1.一般步骤: s1:当两数均为偶数时将其同时除以2至至少一数为奇数为止,记录除掉的所有公因数2的乘积k: s2:如果仍有一数为偶数,连续除以2直至该数为奇数为止: ...

  3. 【leetcode 简单】第四十题 求众数

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...

  4. makefile使用.lds链接脚本以及 $@ ,$^, $,< 解析【转】

    转自:http://www.cnblogs.com/lifexy/p/7089873.html 先来分析一个简单的.lds链接脚本 例1,假如现在有head.c init.c nand.c main. ...

  5. elk系列1之入门安装与基本操作【转】

    preface 我们每天都要查看服务器的日志,一方面是为了开发的同事翻找日志,另一方面是巡检服务器查看日志,而随着服务器数量以及越来越多的业务上线,日志越来越多,人肉运维相当痛苦了,此时,参考现在非常 ...

  6. 选中一行并且选中该行的radio

    $("tr").bind("click",function(){ $("input:radio").attr("checked&q ...

  7. 008 BlockingQueue理解

    原文https://www.cnblogs.com/WangHaiMing/p/8798709.html 本篇将详细介绍BlockingQueue,以下是涉及的主要内容: BlockingQueue的 ...

  8. Linux /etc/cron.d作用(转自 定时任务crontab cron.d)

    原文链接:http://huangfuligang.blog.51cto.com/9181639/1608549 一.cron.d增加定时任务 当我们要增加全局性的计划任务时,一种方式是直接修改/et ...

  9. Webmin忘记密码解决方法,及配置文件介绍

    Webmin忘记Web登陆时候的密码,无法登陆了,Google了一下,基本方法是通过changepass.pl可以修改密码 首先找到changepass.pl这个文件目录 $sudo locate c ...

  10. hdu 4678

    HDU 4768: Flyer 题意: 有N个社团,每个社团三个属性A,B,C,表示会向编号A+k*C的同学发传单(k=0,1,2...  && A+k*C <= B).题目保证 ...