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. 开发人员应该知道的SEO

    搜索引擎是如何工作的 > 如果你有时间,可以读一下谷歌的框架: http://infolab.stanford.edu/~backrub/google.html > 这是一个老的,有些过时 ...

  2. 值传递 & 引用传递

    以下程序的输出结果是? public class Example { String str = new String("good"); char[] ch = { 'a', 'b' ...

  3. Console 程序在任务计划程序无法读写文件

    当我们把Console 程序作为Window计划任务的计划任务的操作的时候,我们明明设置了程序的执行权限或者文件夹的读写权限(尝试了所有权限,各种账号读写权限的切换都不好使),但是当我们有读写操作的时 ...

  4. struts——拦截器

    什么是拦截器 拦截器(Interceptor)是Struts 2的一个强有力的工具,有许多功能都是构建于它之上,如国际化(前两篇博客介绍过).转换器,校验等. 拦截器是动态拦截Action调用的对象. ...

  5. 实现Android操作系统11种传感器介绍

    在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER 1 //加速度 #define ...

  6. [Immutable,js] Immutable.Record() as data models

    The Immutable.js Record() allows you to model your immutable data much like you would model data wit ...

  7. ASP.NET MVC 部分视图(转)

    [部分视图] ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control.我们的页面往往会有许多重用的地方,可以进行封装重用.使用 部分视图 :  1. 可以简写代 ...

  8. CSS 设计彻底研究(三)深入理解盒子模型

    第三章 深入理解盒子模型 盒子模型是CSS控制页面的基础.需要清楚“盒子”的含义是什么,以及盒子的组成.此外,应该理解DOM的基本概念,以及DOM树是如何与一个HTML文档对应的,在此基础上充分理解“ ...

  9. CTL_CODE 宏 详解

    CTL_CODE宏 CTL_CODE:用于创建一个唯一的32位系统I/O控制代码,这个控制代码包括4部分组成: DeviceType(设备类型,高16位(16-31位)), Function(功能2- ...

  10. js 日期天数相加减,格式化yyyy-MM-dd

    参数格式: date :2016-03-02 days:-3(2)当为负数的时候日期往前推,为正数,日期往后推 function addDate(date, days) { var d = new D ...