HDU 2007-11 Programming Contest

Can you find it?

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


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
 
思路:  首先将任意两个数组两两相加,保存到一个数组中,这个数组一定要开大一写,最好是500*500,排序,然后加这个新的数组和第三个数组相加,利用二分的思想进行查找。
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int al[505],an[505],am[505],d[250025];
int main()
{
    int l,n,m,s,flag=1,s1;
    while(~scanf("%d%d%d",&l,&n,&m))
    {
        for(int i=1;i<=l;i++) scanf("%d",&al[i]);
        for(int i=1;i<=n;i++) scanf("%d",&an[i]);
        for(int i=1;i<=m;i++) scanf("%d",&am[i]);
        int k=1;
        for(int i=1;i<=l;i++){
            for(int j=1;j<=n;j++)
                d[k++]=al[i]+an[j];
        }
        sort(d+1,d+k);
        scanf("%d",&s);
        printf("Case %d:
",flag++);
        while(s--)
        {
            bool sign = false;
            scanf("%d",&s1);
            for(int i=1;i<=m;i++)
            {
                int left=1,right=k;
                while(left<=right)
                {
                    int middle = (left+right)/2;
                    if(d[middle]+am[i]==s1) {sign = true; break;}
                    if(d[middle]+am[i]<s1) left = middle+1;
                    else right= middle -1;
                }
            }
            if(sign) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jinhong123/p/8764120.html