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. use_frameworks!和#use_frameworks!的区别、解决Swift项目中use_frameworks!冲突的问题

    use_frameworks!和#use_frameworks!的区别 转自:https://www.jianshu.com/p/0ae58a477459 1. 用cocoapods 导入swift ...

  2. PHP magic_quotes_gpc 和 addslashes解析

    默认情况下,PHP 指令 magic_quotes_gpc 为 on,它主要是对所有的 GET.POST 和 COOKIE 数据自动运行 addslashes().不要对已经被 magic_quote ...

  3. Visual Studio 2015的安装及单元测试练习

    第一部分:Visual Studio 2015的安装 我电脑系统是win10,所以安装的是Visual Studio 2015,安装步骤部分截图如图所示: 1.安装类型选项界面:可以选择默认安装,可以 ...

  4. 北航学堂Android客户端Beta阶段测试报告

    我们已经知道的bug如下: 1.在没有网络的情况下,我们的程序会直接崩溃,没有弹出提醒网络异常的错误,这是个比较严重的bug,我们在6号7号 考试结束之后会进行修改: 有待进行的优化: 1.UI界面的 ...

  5. Java开发异常

    1.org.apache.catalina.LifecycleException 报错信息如下: 警告: Error during context [/wxqhbcloud] restart org. ...

  6. SqlDataAdapter简单介绍 (转)

    From:  http://blog.sobnb.com/u/92/5532.html 一.特点介绍 1.表示用于填充 DataSet 和更新 SQL Server 数据库的一组数据命令和一个数据库连 ...

  7. Java awt项目开发

    通过Java awt 界面上的知识编写的扫雷游戏 代码中有详细的注解 package com.langsin.saolei; import java.awt.Color;import java.awt ...

  8. PAT 1037 在霍格沃茨找零钱

    https://pintia.cn/problem-sets/994805260223102976/problems/994805284923359232 如果你是哈利·波特迷,你会知道魔法世界有它自 ...

  9. Classification Truth Table

    在机器学习中对于分类结果的描述,一般有四种:true positive, true negative, false positive 和 false negative. Precision, Reca ...

  10. [转帖]浪潮openstack的逆袭

    扬长补短 浪潮云平台的逆袭 http://blog.itpub.net/11310314/viewspace-2564083/ 浪潮并不是最早加入OpenStack的国内厂商,却实现同比303.1%的 ...