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 */ ...
随机推荐
- [洛谷P2750] [USACO5.5]贰五语言Two Five
洛谷题目链接:[USACO5.5]贰五语言Two Five 题目描述 有一种奇怪的语言叫做"贰五语言".它的每个单词都由A-Y这25个字母各一个组成.但是,并不是任何一种排列都是一 ...
- PHP与数据库
连接数据库 Connect 访问数据库的数据之前,先要与数据库建立连接,使用mysql_connect()方法与数据库建立连接. mysql_connect()参数 <?php //server ...
- Handlerbars基础笔记
此笔记摘抄于杨元的博客(http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) 引入 <script type=&qu ...
- [php]数组建立方式
1.$a[0]=..; $a[1]=..; $a[2]=..; $a[3]=..; 2.$a=array(1,2,3,4,5); 3.自定义数组 $a['logo']="qq"; ...
- 基本控件文档-UIView属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...
- 【洛谷 P1772】 [ZJOI2006]物流运输(Spfa,dp)
题目链接 \(g[i][j]\)表示不走在\(i\text{~}j\)时间段中会关闭的港口(哪怕只关\(1\)天)从\(1\)到\(m\)的最短路. \(f[i]\)表示前\(i\)天的最小花费.于是 ...
- 大聊Python----装饰器
什么是装饰器? 装饰器其实和函数没啥区别,都是用def去定义的,其本质就是函数,而功能就是装饰其他的函数,说白了就是为其他函数提供附加功能 装饰器有什么作用? 比如你是一个公司的员工,你所写的程序里有 ...
- 蓝色的PC端后台管理界面设计模板——后台
链接:http://pan.baidu.com/s/1o82hXX4 密码:x6le
- ckeditor+ckfinder+java
(java)Ckdeitor+ckfinder整合 第一步:工具下载 首先下载:CKEditor 地址:http://cdeditor.com/dowmload 接着下载CKFinder 地址:htt ...
- HTML表单属性与全局属性
1.全局属性