Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 9180    Accepted Submission(s): 2401

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
 
Author
wangye
 
Source
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2199 2899 2289 1597 1551 

 
  数据结构:二分(折半)查找。
  这道题一开始还在纳闷怎么用二分查找,后来看人家的思路才发现需要用巧办法,做法是先将前两个数列相加产生sab数列,这个时候sab+c = x,那么sab= x-c,每次询问x时,就用x减去c中的所有数,依次在sab数列中查找。
  没想到这样的方法,脑子还是太木,不甘心啊 >_<
  本题代码:
 
 #include <iostream>
#include <algorithm>
using namespace std;
int qn; //sab数组总数
int sab[];
int binsearch(int q[],int n,int k) //二分查找
{
int left=,right=n,mid;
while(left<=right){
mid = (left+right)/;
if(q[mid]==k)
return mid;
if(q[mid]>k)
right = mid - ;
else
left = mid + ;
}
return ;
}
int main()
{
int l,n,m;
int count = ;
while(cin>>l>>n>>m){
qn = ;
int A[],B[],C[];
for(int i=;i<=l;i++){
cin>>A[i];
}
for(int i=;i<=n;i++){
cin>>B[i];
}
for(int i=;i<=m;i++){
cin>>C[i];
}
for(int i=;i<=l;i++)
for(int j=;j<=n;j++)
sab[qn++] = A[i] + B[j]; //产生sab数列
sort(sab+,sab+qn-); //对sab数列进行排序
int s;
cin>>s;
cout<<"Case "<<count++<<":"<<endl;
while(s--){
int t;
cin>>t;
int i;
for(i=;i<=m;i++){
int tt = t - C[i];
if(binsearch(sab,qn-,tt)){ //查找有没有 x-c
cout<<"YES"<<endl;
break;
}
}
if(i>m)
cout<<"NO"<<endl;
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 2141:Can you find it?(数据结构,二分查找)的更多相关文章

  1. hdu 2141 Can you find it?(二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...

  2. 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 ...

  3. 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 ...

  4. Go 数据结构--二分查找树

    Go 数据结构--二分查找树 今天开始一个Go实现常见数据结构的系列吧.有时间会更新其他数据结构. 一些概念 二叉树:二叉树是每个节点最多有两个子树的树结构. 完全二叉树:若设二叉树的高度为h,除第 ...

  5. HDU 2141 Can you find it? (二分)

    题目链接: Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/ ...

  6. hdoj 2141 Can you find it?【二分查找+暴力】

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  7. HDU 5878 I Count Two Three (预处理+二分查找)

    题意:给出一个整数nnn, 找出一个大于等于nnn的最小整数mmm, 使得mmm可以表示为2a3b5c7d2^a3^b5^c7^d2​a​​3​b​​5​c​​7​d​​. 析:预处理出所有形为2a3 ...

  8. 题解报告: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 ...

  9. HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...

  10. 数据结构-二分查找(Binary Search)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

随机推荐

  1. Java之进阶(1) -《Effective Java》

    第1章 引言 第2章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造器(Consider static factory methods instead of constructors) 第2条:遇 ...

  2. OpenCV iOS开发(一)——安装(转)

    OpenCV是一个开源跨平台的的计算机视觉和机器学习库,可以用来做图片视频的处理.图形识别.机器学习等应用.本文将介绍OpenCV iOS开发中的Hello World起步. 安装 OpenCV安装的 ...

  3. Netty的ByteBuf

    https://blog.csdn.net/thinking_fioa/article/details/80795673  netty的ByteBuf知识点

  4. eclipse myeclipse 中的server的配置

    (1)本身tomcat的环境配置就是一个很麻烦的事,一不小心,标点错了,少了,根本就起不来. (2)当同一个PC上.装有多个tomcat的话,更是混乱不堪,不仅每个server.xml文件的port号 ...

  5. Python 数据驱动ddt 使用

    准备工作: pip install ddt 知识点: 一,数据驱动和代码驱动: 数据驱动的意思是  根据你提供的数据来测试的  比如 ATP框架 需要excel里面的测试用例 代码驱动是必须得写代码  ...

  6. IE6支持兼容min-width、max-width CSS样式属性

    IE6支持兼容min-width.max-width CSS样式属性 让IE6支持max-width.IE6支持min-width样式 我们在写CSS的时候,常常会遇到让一个图片或一个布局不能超出设定 ...

  7. web.py+fastcgi+nginx 502错误解决

    用web.py照着官网在服务器上搭好了后台.这次很奇怪地出现了一个Nginx 502 Bad Gateway的错误. 执行上面的kill `pgrep -f "python /path/to ...

  8. PyCharm 在django程序中单独运行py文件

    使用PyCharm开发django程序,发现如果不在命令行而在IDE的django项目中直接运行django程序,发现报错,程序如下:   def main(): from people.models ...

  9. Django And Django-Rest-Framework 异常记录

    1.TypeError: init() takes 1 positional argument but 2 were given

  10. ibatis中in语句参数传入方法

    第一种:传入参数仅有数组,iterate中不能有数组的属性名       <select id="GetEmailList_Test"  resultClass=" ...