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. HDOJ4548_美素数

    简单的素数问题 HDOJ4548_美素数 #include<stdio.h> #include<stdlib.h> #include<math.h> #includ ...

  2. Maven的课堂笔记1

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

  3. HTML使用button的一个小坑

    https://www.w3schools.com/TAGs/att_button_type.asp Definition and Usage The type attribute specifies ...

  4. 传输层中的协议 TCP & UDP

    面向连接的TCP协议 “面向连接”就是在正式通信前必须要与对方建立起连接.比如你给别人打电话,必须等线路接通了.对方拿起话筒才能相互通话.TCP(Transmission Control Protoc ...

  5. hive数据查询

    Fetch task 丢弃了mapreduce的作业的繁重任务,查询方便简单 1.第一种方式 2.linux命令行 3.地3中

  6. CentOS 安全优化

    1.操作系统和数据库系统管理用户身份鉴别信息令应有复杂度要求并定期更换. 配置# vi /etc/login.defs 系统默认配置: PASS_MIN_LEN=5 #密码最小长度 PASS_MAX_ ...

  7. Log4J日志信息配置文件详解

    原文地址: http://blog.csdn.net/wuxintdrh/article/details/78282097 使用log4j 记录日志甚是方便,其提供了两种日志配置方式,log4j.pr ...

  8. VS2008中英文转换

    设置Visual Studio的语言: 工具=>选项=>环境=>区域设置=>语言 如图: 对于英文不好的朋友还是挺好用的

  9. Bootstrap手风琴效果

    前面的话 Bootstrap 框架中 Collapse插件(折叠)其实就是我们常见的手风琴效果.当单击一个触发元素时,在另外一个可折叠区域进行显示或隐藏,再次单击时可以反转显示状态.经典的场景是多个折 ...

  10. CSS实现水平居中的5种思路

    前面的话 水平居中是经常遇到的问题.看似方法较多,条条大路通罗马.但系统梳理下,其实都围绕着几个思路展开.本文将介绍关于水平居中的5种思路 text-align [思路一]:在父元素中设置text-a ...