首页 > 代码库 > Can you find it?

Can you find it?

Can you find it?

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 1140 Accepted Submission(s): 370
 
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 31 2 31 2 31 2 331410
 
Sample Output
Case 1:NOYESNO
 
Author
wangye
 
Source
HDU 2007-11 Programming Contest
 
Recommend
威士忌
 
/*二分查找嘛第一遍想的在三个数组整合到一起查找,但是超内存,想了一下,只整合两个数组的话,时间上虽然复杂了,但是内存小了*/#include<bits/stdc++.h>#define MAX 505using namespace std;long long a[MAX],b[MAX],c[MAX],d[MAX*MAX];int BinarySearch(long long num[],long long end,long long n)/*二分查找*/{    long long l=0,r=end,mid;    while(l<=r)    {        mid=(l+r)/2;        if(num[mid]==n)            return 1;        if(num[mid]>n)            r=mid-1;        else if(num[mid]<n)            l=mid+1;    }    if(num[l]==n)        return 1;    return 0;}int main(){    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);    long long L,N,M,t,n;    int Case=1;    while(scanf("%lld%lld%lld",&L,&N,&M)!=EOF)    {        for(int i=0;i<L;i++)            scanf("%lld",&a[i]);        for(int i=0;i<N;i++)            scanf("%lld",&b[i]);        for(int i=0;i<M;i++)            scanf("%lld",&c[i]);        long long len=0;        for(int i=0;i<L;i++)            for(int j=0;j<N;j++)                    d[len++]=a[i]+b[j];        sort(d,d+len);        scanf("%lld",&t);        printf("Case %d:\n",Case++);        while(t--)        {            scanf("%lld",&n);            int f=0;            for(int i=0;i<M;i++)            {                if(BinarySearch(d,len,n-c[i]))                {                    f=1;                    break;                }                }            if(f)                puts("YES");            else                puts("NO");        }    }}

 

Can you find it?