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

一开始TLE因为是三个for循环在一起,后来将两个for循环分开就没TLE了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
int n,m,l,t,f,tmp,q;
using namespace std;
int a[1000],b[1000],c[1000],d[2555555];
int main()
{int cnt=0;
while(~scanf("%d%d%d",&l,&n,&m))
{f=0;
for(int i=0;i<l;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
scanf("%d",&b[i]);
for(int i=0;i<m;i++)
scanf("%d",&c[i]);
int y=0;
for(int j=0;j<l;j++)
for(int k=0;k<n;k++)
d[y++]=a[j]+b[k];
sort(d,d+y);
scanf("%d",&t);
printf("Case %d:\n",++cnt);
while(t--)
{scanf("%d",&tmp);
for(int i=0;i<m;i++)
{q=tmp-c[i];
int low=0;
int high=y-1;
int mid=(y-1)/2;
while(low<high)
{
if(d[mid]==q)
{f=1;
break;}
if(d[mid]<q)
low=mid+1;
if(d[mid]>q)
high=mid-1;
mid=(high+low)/2;
}
if(d[low]==q || d[high]==q) f=1;
if(f)
break;}
if(f==1)
printf("YES\n");
else
printf("NO\n");
f=0;
}}
return 0;
}

Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏的更多相关文章

  1. OC基础:类的扩展.协议 分类: ios学习 OC 2015-06-22 19:22 34人阅读 评论(0) 收藏

    //再设计一个类的时候,有些方法需要对外公开(接口),有些仅供内部使用. 类的扩展:为类添加新的特征(属性)或者方法 对已知类: 1.直接添加 2.继承(在其子类中添加实例变量和方法) 3.使用ext ...

  2. android开发之this.finish()的使用 分类: android 学习笔记 2015-07-18 19:05 30人阅读 评论(0) 收藏

    在一个Activity用完之后应该将之finish掉,但是,之前在学校里自己摸索着开发时并没有太注意这个问题,因为activity无论是否finish掉对功能的影响貌似都不是那么明显(这是读书时候的观 ...

  3. UI基础:事件.响应链 分类: iOS学习-UI 2015-07-03 19:51 1人阅读 评论(0) 收藏

    UIEvent:事件,是由硬件捕捉的一个代表用户操作操作设备的对象. 事件分三类:触摸事件.晃动事件.远程控制事件. 触摸事件:用户通过触摸设备屏幕操作对象,.输入数据.支持多点触摸,包含1个到多个触 ...

  4. UI基础:UILabel.UIFont 分类: iOS学习-UI 2015-07-01 19:38 107人阅读 评论(0) 收藏

    UILabel:标签 继承自UIView ,在UIView基础上扩充了显示文本的功能.(文本框) UILabel的使用步骤 1.创建控件 UILabel *aLabel=[[UILabel alloc ...

  5. OC基础:Date 分类: ios学习 OC 2015-06-22 19:16 158人阅读 评论(0) 收藏

    NSDate  日期类,继承自NSObject,代表一个时间点 NSDate *date=[NSDate date]; NSLog(@"%@",date);   //格林尼治时间, ...

  6. OC基础:block.字面量 分类: ios学习 OC 2015-06-22 19:08 155人阅读 评论(0) 收藏

    block 块语法,可以用block去保存一段代码,或者封装一段代码. block 实际是由c语言实现的,执行效率很高. block 实际借鉴了函数指针的语法. block,在多线程.异步任务,集合遍 ...

  7. OC基础:NSString NSNumber 分类: ios学习 OC 2015-06-17 17:55 77人阅读 评论(0) 收藏

    OC中的基本数据类型 NSString  在64位系统下,相当于long,在非64位系统下,想当于int. CGFloat   在64位系统下,相当于double,在非64位系统之下,想当与float ...

  8. OC基础:继承.初始化方法,便利构造器 分类: ios学习 OC 2015-06-16 19:27 84人阅读 评论(0) 收藏

    继承: 1.单向继承,一个类只能有一个父类,一个父类可以有多个子类. 2.单向继承,基类(根类)是OSObject 3.子类可以继承父类的属性和方法 当父类的方法不满足子类的需求时,子类可以重写父类的 ...

  9. OC基础:getter和setter,@public @protected @private 分类: ios学习 OC 2015-06-15 19:23 22人阅读 评论(0) 收藏

    @public 1.公开的,公共的,可以在类的内部和外部访问. 2.类的内部:实例变量名 3.类的外部:对象->实例变量名 @protected 1.受保护的,只能在本类和子类中可以访问 2.类 ...

随机推荐

  1. hbase的thriftserver开启

    说明:hbase的thriftserver默认已经编译好,可以使用,不需要跟hadoopthrift一样配置. 要使用Hbase的thrift接口,必须将它的服务启动,命令行为: hbase-deam ...

  2. 04747_Java语言程序设计(一)_第10章_网络与数据库编程基础

    例10.1说明InetAddress类的用法的应用程序. public class Example10_1 { public static void main(String args[]) { try ...

  3. 《Java程序员面试笔试宝典》之Java与C/C++有什么异同

    Java与C++都是面向对象语言,都使用了面向对象思想(例如封装.继承.多态等),由于面向对象有许多非常好的特性(继承.组合等),使得二者都有很好的可重用性. 需要注意的是,二者并非完全一样,下面主要 ...

  4. add BOM to fix UTF-8 in Excel

    fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

  5. 解决一个maven在eclipse中M2_HOME不能调整的问题

    在坚持了许久之后,依然还是没能抵住maven的各种攻击,终于从0开始maven. 开始由于有关小项目,时间紧任务重,没仔细研究maven,便匆匆上手了,导致maven库的位置放在了c盘当前用户的数据目 ...

  6. JSP九大内置对象和四种属性范围解读

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...

  7. Linux UGO和ACL权限管理

    自主访问控制(Discretionary Access Control, DAC)是指对象(比如程序.文件.进程)的拥有者可以任意修改或者授予此对象相应的权限.Linux的UGO(User, Grou ...

  8. animation与transition

    animation 动画,无法直接决定开始时间 demo1 @-webkit-keyframes demo-animation1{ 0% { -webkit-transform:translate3d ...

  9. Android-现场保护

    现场保护 当一个活动进入到了停止的状态,是有可能被系统回收的,我们都学过Activity的生命周期 当活动处于onPause() ,onStop() ,onDestroy() 三种状态时程序可能会被A ...

  10. Python学习笔记5(字符串与正则表达式)

    1.字符串 1.1字符串的格式化 #格式化语法 "%s" % str1 "%s %s" % (str1,str2) #格式化字符串 str1 = "v ...