码迷,mamicode.com
首页 > 其他好文 > 详细

Can you find it?——[二分查找]

时间:2016-03-22 13:51:19      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

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,问是否存在i,j使满足A[i]+B[j]=x的?最快的方法是枚举A,然后在B中二分查找

x-A。现在回到这个问题,这道题给了三组序列A,B,C如何查找呢?我们不妨将A,C两数组合并成新数组LN,LN中每个元素都是Ai+Bj的和。然后枚举B,在LN中二分查找x-B。

*这里有个细节:由于x为32位整数,当A+B>INT32_MAX时,可以不用加入LN.

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <time.h>
 5 using namespace std;
 6 #define clock__ printf("%f\n",double(clock())/CLOCKS_PER_SEC);
 7 #define maxn 500
 8 #define INT_32_MAX ((1<<31)-1)
 9 typedef long long LL;
10 int A[maxn+3],B[maxn+3],C[maxn+3];
11 int L,M,N,S;
12 int LN[maxn*maxn+5];
13 int h;
14 
15 bool search_LN(int a,int b,int x){
16     int len=b-a;
17     int mid=a+len/2;
18     if(len==1){
19         if(LN[a]==x) return true;
20         else return false;
21     }
22     if(LN[mid]==x) return true;
23     else if(LN[mid]>x) return search_LN(a, mid, x);
24     else return search_LN(mid, b, x);
25 }
26 
27 int main() {
28 
29     int T=0;
30     while(scanf("%d%d%d",&L,&M,&N)==3){
31         printf("Case %d:\n",++T);
32         for(int i=0;i<L;i++)
33             scanf("%d",&A[i]);
34         for(int i=0;i<M;i++)
35             scanf("%d",&B[i]);
36         for(int i=0;i<N;i++)
37             scanf("%d",&C[i]);
38         h=0;
39         for(int i=0;i<L;i++)
40             for(int j=0;j<N;j++){
41                 if(LL(A[i])+C[j]<=INT_32_MAX)
42                 LN[h++]=A[i]+C[j];
43             }
44         sort(LN, LN+h);
45         scanf("%d",&S);
46         for(int i=1;i<=S;i++){
47             int x;
48             scanf("%d",&x);
49             bool ok=0;
50             for(int j=0;!ok&&j<M;j++){
51                 if(search_LN(0,h, x-B[j]))
52                     ok=1;
53             }
54             if(ok)printf("YES\n");
55             else printf("NO\n");
56         }
57     }
58     //clock__
59     return 0;
60 }

 

Can you find it?——[二分查找]

标签:

原文地址:http://www.cnblogs.com/Kiraa/p/5306109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!