Can you find it?

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

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
题目大意:给你三个数列,然后给你若干个sum,问你能否从每个数列中各取出一个数,使得它们的和为sum,如果可以,输出YES,
反之输出NO。
思路分析:很明显暴力做会超时,应该采用先合并数组然后二分查找,时间复杂度降到了nlogn,但是在实现的时候还是有几点要注意,
首先是输出格式,先 输出Case,然后再输入s,另外由于粗心大意,low和high的声明写到了for循环外面,wa到死,沧桑。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int inf=0xffffff;
const int maxn=500+50;
__int64 a[maxn],b[maxn],c[maxn];
__int64 f[maxn*maxn];
int kase=0;
int main()
{
    int L,N,M;
    __int64 s,sum;
    while(scanf("%d%d%d",&L,&N,&M)!=EOF)
    {
        int num=0;
        for(int i=0;i<L;i++)
            scanf("%I64d",&a[i]);
        for(int i=0;i<N;i++)
            scanf("%I64d",&b[i]);
        for(int i=0;i<M;i++)
            scanf("%I64d",&c[i]);
        for(int i=0;i<L;i++)
        {
            for(int j=0;j<N;j++)
            {
                f[num++]=a[i]+b[j];
            }
        }
        sort(f,f+num);
       printf("Case %d:\n",++kase);
        scanf("%I64d",&s);
        while(s--)
        {
            scanf("%I64d",&sum);
            int flag=0;
        for(int i=0;i<M;i++)
        {
             int low=0,high=L*N-1;
            if(flag) break;
            while(low<=high)
            {
            int mid=(high+low)/2;
            if(f[mid]==sum-c[i])
            {
                flag=1;
                break;
            }
            else if(f[mid]<sum-c[i]) low=mid+1;
             else  high=mid-1;
            }
        }
         if(flag) cout<<"YES"<<endl;
         else cout<<"NO"<<endl;
        }
    }
    return 0;
}
tip:直接调用二分查找函数binary_search也可以解决

hdu 2143 数组合并 二分的更多相关文章

  1. 关于table动态添加数据 单元格合并 数组合并

    var newArr = [ {"BranchID":1,"BranchName":"城二","BranchFullName&qu ...

  2. go语言:多个[]byte数组合并成一个[]byte

    场景:在开发中,要将多个[]byte数组合并成一个[]byte,初步实现思路如下: 1.获取多个[]byte长度 2.构造一个二维码数组 3.循环将[]byte拷贝到二维数组中 package gst ...

  3. 论php数组合并

    注:尽量不要在循环中操作数据库. 1.两个一维数组合并成一个一维数组 $a = array('morning','afternoon','night'); $b = array('breakfast' ...

  4. PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧

    PHP中两个数组合并可以使用+或者array_merge,但之间还是有区别的,而且这些区别如果了解不清楚项目中会要命的! 主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 ...

  5. JS数组键值,数组合并,

    eg: var arr = [] arr.test = '测试'; arr.push(1); arr.push(2); arr.obj = '对象'; console.log(arr);// [ 1, ...

  6. PHP数组合并 array_merge 与 + 的差异

    在PHP数组合并出过几次问题都没记住,写下来加强一点记忆 PHP数组在合并时,使用 array_merge 与 + 的差异: 1.array_merge(array $array1 [, array  ...

  7. Python数组合并

    Python数组合并 a = [1, 2] b = [3, 4] c = a + b print(c) # [1, 2, 3, 4]

  8. PHP数组合并的常见问题

    一维数组的合并 <?php $arr1=array("a","b","c"); $arr2=array("c",& ...

  9. php二位数组合并

    转自:http://www.cnblogs.com/losesea/archive/2013/06/14/3134900.html 题目:有以下2个二维数组 1$a=Array(0 => Arr ...

随机推荐

  1. [转]Mac App distribution in App Store

    Mac程序的大包上传和iOS的有些许不同,因为Mac app既可以上传到store,也可以不通过store供人下载.因此,code sign和provision要根据情况(开发,release< ...

  2. uva 10609 - Fractal

    题目大意:给出A,B两个点的坐标,以及T,每次找到A.B的四等分点C,D,然后以AB/2为边长,C,D为顶点,构建一个等边三角形,E为另外一个顶点,然后再对C,E:E,D做同样的操作,直到构建的等边三 ...

  3. jq:get获取json数据并以表格形式生成到页面

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 转载:spring ,struct2 在 web.xml中的配置

    转载网址:http://blog.sina.com.cn/s/blog_4c6e822d0102dv63.html <!-- Struts2 need begin-->  <filt ...

  5. Vim的snipMate插件

    介绍终于发现了一个插件,对于Vim下代码块的自动补全支持的很好.给大家推荐snipMate. snipMate可以帮助您在vim上实现类似Textmate的功能,自动代码块的能力非常强大,而且代码块是 ...

  6. Swift 可选类型-备

    我们先看看如下代码: var n1: Int = 10 n1 = nil         //编译错误 let str: String = nil    //编译错误 Int和String类型不能接受 ...

  7. PCB Layout 中的高频电路布线技巧

    1.多层板布线 高频电路往往集成度较高,布线密度大,采用多层板既是布线所必须,也是降低干扰的有效手段.在PCB Layout阶段,合理的选择一定层数的印制板尺寸,能充分利用中间层来设置屏蔽,更好地实现 ...

  8. 自制单片机之十三……时钟IC_DS1302

    在网上看了很久,发现初学者最有兴趣的就是DS1302时钟电路,也很自然,它是个做出来就让你觉得最实用的电路了,但实际上制做上并不简单,首先你要让你的显示部分(不管是数码管还是LCD)调试通过.然后把D ...

  9. iptables 添加,删除,查看,修改

      iptables是linux系统自带的防火墙,功能强大,学习起来需要一段时间,下面是一些习iptables的时候的记录.如果iptables不熟悉的话可以用apf,是一款基于iptables的防火 ...

  10. windows下重命名一个带有前缀"."dot字符的名字的错误问题

    如果用正常的右键重命名那么肯定会报错的,比如: 有一个名为project的文件,我想把它命名为.project,加了个前缀dot.然后window就报错了,弹出个对话框让“你必须输入一个文件名”.它可 ...