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): 8506    Accepted Submission(s): 2216


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:  2289 1597 1551 2298 1016 
 
 1 //375MS    1228K    1325 B    C++
 2 /*
 3     
 4     题意:
 5         给出三行数和Y,问每是否存在行中一个数令 
 6            Ai+Bj+Ck==Y 
 7         成立
 8     
 9     二分法:
10         笨笨的分析错时间复杂度了
11         先将两组数据合并,然后排序,然后进行二分
12         时间复杂度:O(n*n*lgn)...
13          
14 */
15 #include<stdio.h>
16 #include<string.h>
17 #include<stdlib.h>
18 #define N 505
19 int a[N],b[N],c[N],d[N*N];
20 int l,n,m;
21 int cmp(const void*a,const void*b)
22 {
23     return *(int*)a-*(int*)b;
24 }
25 int main(void)
26 {
27     int t,s;
28     int cas=1;
29     while(scanf("%d%d%d",&l,&n,&m)!=EOF)
30     {
31         for(int i=0;i<l;i++) scanf("%d",&a[i]);
32         for(int i=0;i<n;i++) scanf("%d",&b[i]);
33         for(int i=0;i<m;i++) scanf("%d",&c[i]);
34         int cnt=0;
35         for(int i=0;i<n;i++)
36             for(int j=0;j<m;j++)
37                 d[cnt++]=b[i]+c[j];
38         qsort(a,l,sizeof(a[0]),cmp);
39         qsort(d,cnt,sizeof(d[0]),cmp);
40         scanf("%d",&t);
41         printf("Case %d:
",cas++);
42         while(t--){
43             int flag=0;
44             scanf("%d",&s);
45             for(int i=0;i<l;i++){
46                 int tl=0,tr=cnt-1;
47                 int tt=s-a[i];
48                 while(tl<tr){
49                     int mid=(tl+tr)/2;
50                     if(d[tl]==tt || d[tr]==tt || d[mid]==tt){ //注意此处判断 
51                         flag=1;break;
52                     }else if(d[mid]<tt)
53                         tl=mid+1;
54                     else tr=mid-1;
55                 }
56                 if(flag) break;
57             }
58             if(flag) puts("YES");
59             else puts("NO");
60         }
61     }
62     return 0;
63 } 
原文地址:https://www.cnblogs.com/GO-NO-1/p/3427076.html