题解报告:hdu 2141 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
解题思路:典型的二分搜索,先将a、b两两求和并排好序,然后二分搜索是否能找到x-c[j]这个值即可。 
AC代码(358ms):
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=505;
 4 int l,n,m,q,cnt=1,x,a[maxn],b,c[maxn];bool flag;
 5 vector<int> vec;
 6 int main(){
 7     while(~scanf("%d%d%d",&l,&n,&m)){
 8         vec.clear();
 9         for(int i=1;i<=l;++i)scanf("%d",&a[i]);
10         for(int i=1;i<=n;++i){
11             scanf("%d",&b);
12             for(int j=1;j<=l;++j)
13                 vec.push_back(a[j]+b);
14         }
15         sort(vec.begin(),vec.end());
16         for(int i=1;i<=m;++i)scanf("%d",&c[i]);
17         scanf("%d",&q);printf("Case %d:\n",cnt++);
18         while(q--){
19             scanf("%d",&x);flag=false;
20             for(int j=1;j<=m;++j){
21                 int pos=lower_bound(vec.begin(),vec.end(),x-c[j])-vec.begin();
22                 if(pos!=l*n&&vec[pos]==x-c[j]){flag=true;break;}
23             }
24             if(flag)puts("YES");
25             else puts("NO");
26         }
27     }
28     return 0;
29 }

 

posted @ 2018-08-27 21:47  霜雪千年  阅读(290)  评论(0编辑  收藏  举报