(binary_search) Can you find it hdu2141
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的更多相关文章
- 数据结构和算法:Python实现二分查找(Binary_search)
在一个列表当中我们可以进行线性查找也可以进行二分查找,即通过不同的方法找到我们想要的数字,线性查找即按照数字从列表里一个一个从左向右查找,找到之后程序停下.而二分查找的效率往往会比线性查找更高. 一. ...
- c++ 二分法查找(binary_search)
#include <iostream> // cout #include <algorithm> // binary_search, sort #include <vec ...
- 传智播客C++视频学习笔记(5)
#include <iostream> using namespace std; void swapInt(int& a, int& b) { int temp = a; ...
- python函数(五)
函数 1.函数基本语法及特性 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况,当cpu\memory\disk等指标的使用量超过阀值时即发邮件报警, 你掏空了所有的知识量,写出了以下代码 ...
- c++中级 STL基础学习(二)
deque 和vector差不多,可以在前端后端插入,一般用deque取代vector,vector只能在后端插入push_back().deque还可以push_front(),但是deque后端插 ...
- c++容器使用总结(转载)
目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...
- Python数据结构应用4——搜索(search)
Search是数据结构中最基础的应用之一了,在python中,search有一个非常简单的方法如下: 15 in [3,5,4,1,76] False 不过这只是search的一种形式,下面列出多种形 ...
- 05--STL序列容器(List)
一:List双向链表简介 list是一个双向链表容器,可高效地进行插入删除元素. list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符.It++(ok) it+5(err)list不 ...
- 史上最全python面试题详解 (二)(附带详细答案(关注、持续更新))
23.re的match和search区别? re.match()从开头开始匹配string. re.search()从anywhere 来匹配string. # 多行模式>>> re ...
随机推荐
- Python 可调用对象
除了用户定义的函数,调用运算符(即 ())还可以应用到其他对象上.如果想判断对象能否调用,可以使用内置的 callable() 函数.Python 数据模型文档列出了 7 种可调用对象.(1)用户定义 ...
- 【CV】ICCV2015_Unsupervised Visual Representation Learning by Context Prediction
Unsupervised Visual Representation Learning by Context Prediction Note here: it's a learning note on ...
- 网络:LVS负载均衡原理
LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director Server上,然后它把用户的请求根据设置好的调度算法,智能均衡地分发到后端真正服务器(real server)上.为 ...
- Xshell连接到centos提示Could not connect to (port 22): Connection failed
关于XShell连接虚拟机中的centos系统的问题,在连接的时候报错如下: 一开始以为是系统的问题,但是搞了很久,才发现是虚拟机这个软件本身的问题,的确坑啊!所以解决方法也很简单.在编辑菜单那里打开 ...
- node基础 npm、module、exports、require
module 模块.包:可以认为是一个代码包,package,提供特定的功能(暴露给外界接口,让外界调用) exports 输出.导出:导出模块中的各种类型的变量,以及各种方法,导出之后,才可以被外界 ...
- 深度学习中 --- 解决过拟合问题(dropout, batchnormalization)
过拟合,在Tom M.Mitchell的<Machine Learning>中是如何定义的:给定一个假设空间H,一个假设h属于H,如果存在其他的假设h’属于H,使得在训练样例上h的错误率比 ...
- ASP.NET MVC与WebForm对比
MVC优点:1.分离更彻底,分层清晰,易于维护和扩展.2.验证更加方便快捷.3.无ViewState,页面更加干净4.路由更容易定义url,对SEO比较好.5.强类型VIEW实现,更安全高效. Web ...
- VS2008中英文转换
设置Visual Studio的语言: 工具=>选项=>环境=>区域设置=>语言 如图: 对于英文不好的朋友还是挺好用的
- 【Java】初始化
默认域初始化 如果在构造器中没有显示地给域赋予初值,那么就会被自动赋予默认值:数值为0,布尔值为false,对象引用为null. 无参数构造器 很多类都包含一个无参数的构造函数,对象由无参数构造函数创 ...
- 洛谷 P4294 [WC2008]游览计划
题目链接 不是很会呢,但似乎抄了题解后有点明白了 sol:状态DP显然,其实是要构建一棵最小生成树一样的东西,我自己的理解(可能不是很对哦希望多多指教)f[x][y][zt]就是到x,y这个点,状态为 ...