[ACM] 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 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

HDU 2007-11 Programming Contest

 

解题思路:

给出几个数串,给定一个数,问从这几个数串中分别取出一个数,能否相加的和正好等于给定的那个数。思路为假设三个数串 1 2      4 5       7 8,给定的数字为12,我们把前两个数串中的任意两个数的和保存在一个sum数组中,及 1+4=5   1+5=6   2+4=6  2+5=7,sum数组中存的是5 6 6 7,遍历第三个数串,另给定的数字12减去第三个数串的数,及temp=12-c[i],然后采用二分查找的方法在sum数组中查找是否有temp这个数。如果有的话就符合题意,break掉。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
long long a[502],b[502],c[502];
long long sum[502*502];

bool search(long long t[],int l,int r,long long key)//二分
{
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(t[mid]==key)
            return true;
        else if(t[mid]<key)
            l=mid+1;
        else
            r=mid-1;
    }
    return false;
}

int main()
{
    int l,n,m;
    int cc=1;
    while(cin>>l>>n>>m)
    {
        int len=0;
        for(int i=1;i<=l;i++)
            scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&b[i]);
        for(int i=1;i<=m;i++)
            scanf("%I64d",&c[i]);
        for(int i=1;i<=l;i++)
            for(int j=1;j<=n;j++)
            sum[len++]=a[i]+b[j];//sum数组保存a与b数组元素所有和的情况
        sort(c+1,c+1+m);//从小到大排序  注意c数组是从1开始的,sum是从0开始的
        sort(sum,sum+len);
        cout<<"Case "<<cc<<":"<<endl;
        int t,x;
        cin>>t;
        for(int i=1;i<=t;i++)
        {
            cin>>x;
            if(sum[0]+c[1]>x||sum[len-1]+c[m]<x)//不符合题意的情况,最小+最小大于总和x,最大+最大小于总和x
            {
                cout<<"NO"<<endl;
                continue;
            }
            int j;
            for( j=1;j<=m;j++)
            {
                long long temp=x-c[j];//在sum数组中寻找temp
                if(search(sum,0,len-1,temp))
                {
                    cout<<"YES"<<endl;
                    break;
                }
            }
            if(j>m)
                cout<<"NO"<<endl;
        }
        cc++;
    }
    return 0;
}


 

posted @ 2014-02-24 16:07  同学少年  阅读(168)  评论(0编辑  收藏  举报