HDU 2141 Can you find it?

          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
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int p[3][505];
 6 long long temp[250010];
 7 int s[1005];
 8 
 9 /*int search(int l,int r,int num)
10 {
11     while(l<=r)
12     {
13         int mid=(l+r)/2;
14         if(temp[mid]==num)
15         return mid;
16         else if(temp[mid]>num)
17         r=mid-1;
18         else
19         l=mid+1;
20     }
21     return l;
22 }*/
23 
24 int main()
25 {
26     //freopen("in.txt","r",stdin);
27     int a,b,c,i,j,k,n;
28     int m=0;
29     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
30     {
31         printf("Case %d:
",++m); 
32         for(i=0;i<a;i++)
33         scanf("%d",&p[0][i]);
34         for(i=0;i<b;i++)
35         scanf("%d",&p[1][i]);
36         for(i=0;i<c;i++)
37         scanf("%d",&p[2][i]);
38         scanf("%d",&n);
39         for(i=0;i<n;i++)
40         scanf("%d",&s[i]);
41         int k=-1;
42         for(i=0;i<b;i++)
43         for(j=0;j<c;j++)
44         temp[++k]=p[1][i]+p[2][j];
45         sort(temp,temp+k);
46         for(j=0;j<n;j++)
47         {
48             int target;
49             int ans;
50             for(i=0;i<a;i++)
51             {
52                 target=s[j]-p[0][i]; 
53                 int ans=lower_bound(temp,temp+k,target)-temp;
54                 //ans=search(0,k,target);
55                 if(temp[ans]==target) 
56                 break;
57             }
58             if(i!=a)
59             printf("YES
");
60             else
61             printf("NO
");    
62         }
63     } 
64     
65 }
 
原文地址:https://www.cnblogs.com/homura/p/4690231.html