Can you find it? HDU - 2141 (二分查找)
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
//该题的思想是先合并前两组,然后用题目中的x减去第3组的值
//然后在合并组里面二分查找,看是否能找到一个值与x减去第三组的值相等
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int INF=1e9+7;
const int maxn=510;
typedef long long ll;
int l,n,m,S;
int a[maxn],b[maxn],c[maxn],ab[maxn*maxn];
int BinarySearch(int ab[],int h,int t)//二分查找
{
int left=0;
int right=h-1;
int mid=(left+right)/2;
while(left<=right)
{
mid=(left+right)/2;
if(ab[mid]==t)
return 1;
else if(ab[mid]>t)
right=mid-1;
else if(ab[mid]<t)
left=mid+1;
}
return 0;
}
int main()
{
int cot=1;
int i,j,k,h,x;
while(scanf("%d %d %d",&l,&n,&m)!=EOF)
{
h=0;
for(i=0; i<l; i++)
scanf("%d",&a[i]);
for(j=0; j<n; j++)
scanf("%d",&b[j]);
for(k=0; k<m; k++)
scanf("%d",&c[k]);
for(i=0; i<l; i++)
for(j=0; j<n; j++)
ab[h++]=a[i]+b[j];
sort(ab,ab+h);
printf("Case %d:\n",cot++);
scanf("%d",&S);
for(int s=0; s<S; s++)
{
scanf("%d",&x);
int flag=0;
for(k=0; k<m; k++)
{
int t=x-c[k];
if(BinarySearch(ab,h,t))
{
printf("YES\n");
flag=1;
break;
}
}
if(!flag) printf("NO\n");
}
}
return 0;
}
Can you find it? HDU - 2141 (二分查找)的更多相关文章
- Can you find it?(hdu 2141 二分查找)
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- Equations(hdu 1496 二分查找+各种剪枝)
Equations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Pie(hdu 1969 二分查找)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- hdu 2141 Can you find it?(二分查找)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...
- hdu 2141:Can you find it?(数据结构,二分查找)
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- hdu 2141 Can you find it?(二分查找变例)
Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...
- HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...
- HDU 2141 Can you find it? (二分)
题目链接: Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/ ...
- 二分查找 HDOJ 2141 Can you find it?
题目传送门 /* 题意:给出一个数,问是否有ai + bj + ck == x 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x */ ...
随机推荐
- What are the advantages of different classification algorithms?
What are the advantages of different classification algorithms? For instance, if we have large train ...
- 30款基于 jQuery & CSS3 的加载动画和进度条插件
我们所生活每一天看到的新技术或新设计潮流的兴起,Web 开发正处在上升的时代.HTML5 & CSS3 技术的发展让 Web 端可以实现的功能越来越强大. 加载动画和进度条使网站更具吸引力.该 ...
- spring 添加controller返回值绑定
@EnableWebMvc @Configuration public class Config { @Autowired private RequestMappingHandlerAdapter h ...
- js设置html区域隐藏和显示
if(message != "指派") { document.getElementById("appoint").style.display="non ...
- 使用chardet判断编码方式
1. chardet是什么 chardet是python中比较常用的一个编码方式检测库,需要注意的是它只检测并返回检测结果,并不负责对原数据做什么处理. 可以使用PIP命令安装: pip instal ...
- JS日历控件特效代码layDate
https://www.js-css.cn/a/jscode/date/2015/0405/1461.html
- 南邮PHP反序列化
题目如下: <?php class just4fun { var $enter; var $secret; } if (isset($_GET['pass'])) { $pass = $_GET ...
- sql server 2008 r2 产品密钥
数据中心版:PTTFM-X467G-P7RH2-3Q6CG-4DMYBDDT3B-8W62X-P9JD6-8MX7M-HWK38==================================== ...
- redis+cookie+json+filter实现单点登录
目录: 1.项目集成redis客户端jedis 引入Jedis pom 2.redis连接池构建及调试 1)JedisPoolConfig源码解析 2)JedisPool源码解析 3)JedisPoo ...
- The hub and spoke model 轮辐模型/辐射模型
最近一些文档中提到The Hub and Spoke Model,这里mark一下.hub表示轮毂,spoke表示轮辐,轮辐模型是简化网络路由的一套中心化的体系,广泛应用于航空.货运.快递以及网络技术 ...