Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)

Total Submission(s): 35782    Accepted Submission(s): 8831

Problem 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

思路:

此处可以用暴力求解法以及二分查找法(会节省更多时间)。而二分查找是有对应的STL的。此处是可以用binary_search,是在有序数列中确定给定的元素是否存在。

#include <iostream>
#include <algorithm>
using namespace std;
int a[],b[],c[],sum[],s[];
int main()
{
int l,n,m,i,j,s1,T=;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
T++;
for(i=;i<l;i++)
scanf("%d",&a[i]);
for(i=;i<n;i++)
scanf("%d",&b[i]);
for(i=;i<m;i++)
scanf("%d",&c[i]);
for(i=;i<l;i++)
{
for(j=;j<n;j++)
sum[n*i+j]=a[i]+b[j];
}
sort(sum,sum+l*n);
scanf("%d",&s1);
for(i=;i<s1;i++)
scanf("%d",&s[i]);
printf("Case %d:\n",T);
for(i=;i<s1;i++)
{
for(j=;j<m;)
{
if(binary_search(sum,sum+l*n,s[i]-c[j]))
break;
j++;
}
if(j!=m)
printf("YES\n");
else
printf("NO\n");
}
}
return ;
}

(binary_search) Can you find it hdu2141的更多相关文章

  1. 数据结构和算法:Python实现二分查找(Binary_search)

    在一个列表当中我们可以进行线性查找也可以进行二分查找,即通过不同的方法找到我们想要的数字,线性查找即按照数字从列表里一个一个从左向右查找,找到之后程序停下.而二分查找的效率往往会比线性查找更高. 一. ...

  2. c++ 二分法查找(binary_search)

    #include <iostream> // cout #include <algorithm> // binary_search, sort #include <vec ...

  3. 传智播客C++视频学习笔记(5)

    #include <iostream> using namespace std; void swapInt(int& a, int& b) { int temp = a; ...

  4. python函数(五)

    函数 1.函数基本语法及特性 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况,当cpu\memory\disk等指标的使用量超过阀值时即发邮件报警, 你掏空了所有的知识量,写出了以下代码 ...

  5. c++中级 STL基础学习(二)

    deque 和vector差不多,可以在前端后端插入,一般用deque取代vector,vector只能在后端插入push_back().deque还可以push_front(),但是deque后端插 ...

  6. c++容器使用总结(转载)

    目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...

  7. Python数据结构应用4——搜索(search)

    Search是数据结构中最基础的应用之一了,在python中,search有一个非常简单的方法如下: 15 in [3,5,4,1,76] False 不过这只是search的一种形式,下面列出多种形 ...

  8. 05--STL序列容器(List)

    一:List双向链表简介 list是一个双向链表容器,可高效地进行插入删除元素. list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符.It++(ok) it+5(err)list不 ...

  9. 史上最全python面试题详解 (二)(附带详细答案(关注、持续更新))

    23.re的match和search区别? re.match()从开头开始匹配string. re.search()从anywhere 来匹配string. # 多行模式>>> re ...

随机推荐

  1. .apply()用法和call()的区别

    Js apply方法详解我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里 ...

  2. jsp获取传过来的值

    request.setCharacterEncoding("utf-8"); String credit=request.getParameter("credit&quo ...

  3. PHP使用echo输出标签设置CSS样式问题

    使用php是可以输出HTML标签的,这为页面设计带来很大方便. 在此记录php输出标签设置CSS样式的问题: echo可使用''.""或你不用引号,如果想要输出带CSS样式的HTM ...

  4. Sprint 冲刺第三阶段第一天

    1.今晚我在整理之前的代码,检查细节,然后发现游戏要返回上一界面竟然出现了问题“项目停止运行”,仔细检查没办法解决,后来百度可能是因为修改了之前文件的名字,可在AndroidManifest.xml中 ...

  5. mysql数据库忘记密码时如何修改

    工具/原料 mysql数据库 cmd命令行 打开mysql.exe和mysqld.exe所在的文件夹,复制路径地址 打开cmd命令提示符,进入上一步mysql.exe所在的文件夹

  6. Maven的课堂笔记1

    1 什么是maven? Maven是一个跨平台的项目管理工具,主要用于基于java平台的项目构建,依赖管理. Clean  compile  test  package  install   run ...

  7. HTML5的placeHolder在IE9下workaround引发的Bug(按下葫芦起了瓢)

    详见StackOverFlow的:Simple jQuery form Validation: Checking for empty .val() failing in ie9 due to plac ...

  8. back to top 回到顶部按钮 css+js

    效果 html <p id="back-to-top"><a href="#top"><span></span> ...

  9. 搭建Spark所遇过的坑

    一.经验 1.Spark Streaming包含三种计算模式:nonstate .stateful .window 2.kafka可通过配置文件使用自带的zookeeper集群 3.Spark一切操作 ...

  10. 剑指offer:滑动窗口的最大值

    滑动窗口的最大值 题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值 ...