Can you find it?

Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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
 
 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 #define K 505
 5 int LN[K*K];
 6 int judge(int LN[],int h,int t)
 7 {
 8     int left,right,mid;
 9     left=0;
10     right=h-1;
11     mid=(left+right)/2;
12     while(left<=right)
13     {
14         mid=(left+right)/2;
15         if(LN[mid]==t)
16           return 1;
17         else if(LN[mid]>t)
18           right=mid-1;
19         else if(LN[mid]<t)
20           left=mid+1;
21     }
22     return 0;
23 }
24 int main()
25 {
26     int i,j,count=1,q;
27     __int32 L[K],N[K],M[K],S,n,m,l;
28     while(scanf("%d%d%d",&l,&n,&m)!=EOF)
29     {
30         int h=0;
31         for(i=0;i<l;i++)
32          scanf("%d",&L[i]);
33         for(i=0;i<n;i++)
34           scanf("%d",&N[i]);
35         for(i=0;i<m;i++)
36           scanf("%d",&M[i]);
37         for(i=0;i<l;i++)
38           for(j=0;j<n;j++)
39            LN[h++]=L[i]+N[j];
40         sort(LN,LN+h); 
41         scanf("%d",&S);
42         printf("Case %d:\n",count++);
43         for(i=0;i<S;i++)
44         {
45             scanf("%d",&q);
46             int p=0; 
47             for(j=0;j<m;j++)
48             {
49                 int a=q-M[j];
50                 if(judge(LN,h,a)) 
51                 {
52                     printf("YES\n");
53                     p=1;
54                     break;
55                 }
56             }
57             if(!p) 
58               printf("NO\n");
59         }
60     }
61     return 0;
62 }

 

posted @ 2016-07-27 08:52  菜鸟龙*  阅读(298)  评论(0编辑  收藏  举报