Can you find it? HDU

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:
",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
");
                    flag=1;
                    break;
                }
            }
            if(!flag) printf("NO
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nyist-xsk/p/7264842.html