hdu 2141 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): 9266    Accepted Submission(s): 2418


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
 
Author
wangye
 
Source
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2899 2289 1551 2298 1399 
 
 1 /*
 2 题意:Ai+Bj+Ck = X;
 3            其中Ai 属于数组a[ ] , Bj 属于数组b[ ], Ck属于数组 C[ ] 的元素。
 4            求是否能找到满足X。而且X个数有1000,A,B,C个数为500.
 5            思路1.  枚举 X-Ck ,Bj,用二分找Ai是否存在。
 6                         时间复杂度 f(    1000*500*500*log(500)       )
 7            思路2.  Ai+Bj,看成一项。用数组保存tom[ 500*500+1];
 8                         枚举 X-Ck,用二分找tom[ ];
 9                         时间复杂度 f(     1000*500*log(500*500)        )
10 
11 */
12 
13 #include<iostream>
14 #include<stdio.h>
15 #include<cstring>
16 #include<cstdlib>
17 #include<algorithm>
18 using namespace std;
19 
20 int l[502],n[502],m[502];
21 int a[250003];
22 
23 int EF(int l,int r,int x)
24 {
25     int mid;
26     while(l<=r)  //  is  not  while(l<r)  !!
27     {
28         mid=(l+r)/2;
29         if( a[mid]>x)
30             r=mid-1;
31         else if( a[mid]<x)
32             l=mid+1;
33         else return 1;
34     }
35     return -1;
36 }
37 int main()
38 {
39     int i,j;
40     int L,N,M,S,num=0,x,k,len,t;
41     while(scanf("%d%d%d",&L,&N,&M)>0)
42     {
43         for(i=1;i<=L;i++)
44             scanf("%d",&l[i]);
45         for(i=1;i<=N;i++)
46             scanf("%d",&n[i]);
47         for(i=1;i<=M;i++)
48             scanf("%d",&m[i]);
49         len=L*N;t=0;
50         for(i=1;i<=L;i++)
51         for(j=1;j<=N;j++)
52         {
53             a[++t]=l[i]+n[j];
54         }
55         sort(a+1,a+1+len);
56         scanf("%d",&S);
57         printf("Case %d:
",++num);
58         while(S--)
59         {
60             scanf("%d",&x);
61             for(i=1;i<=M;i++)
62             {
63                 k=EF(1,len,x-m[i]);
64                  if( k==1 ) break;
65             }
66             if( k==-1) printf("NO
");
67             else printf("YES
");
68         }
69     }
70     return 0;
71 }
 
原文地址:https://www.cnblogs.com/tom987690183/p/3561623.html