hdu 2141(二分)
//hdu 2141
/*主要题意:分别给l,n,m个a,b,c,要求是否存在ai+bj+ck = x
思路:列出所有ai与bj相加的结果,将与ck相加的结果与M进行比较*/
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 bool cmp(int a,int b) 5 { 6 return a < b; 7 } 8 __int64 a[505],b[505],c[505]; 9 __int64 k[3000000]; 10 int main() 11 { 12 int i,j,t; 13 int n,m; 14 int s1,s2,s3; 15 int p = 1; 16 __int64 l,r,mid; 17 int f; 18 19 while(~scanf("%d %d %d",&s1,&s2,&s3)) 20 { 21 for(i = 0;i < s1;i ++) 22 { 23 scanf("%I64d",&a[i]); 24 } 25 for(i = 0;i < s2;i ++) 26 { 27 scanf("%I64d",&b[i]); 28 } 29 for(i = 0;i < s3;i ++) 30 { 31 scanf("%I64d",&c[i]); 32 } 33 t = 0; 34 for(i = 0;i < s1;i ++) 35 { 36 for(j = 0;j < s2;j ++) 37 { 38 k[t ++] = a[i] + b[j]; 39 } 40 } 41 sort(k,k + t,cmp); 42 scanf("%d",&n); 43 printf("Case %d:\n",p ++); 44 for(i = 0;i < n;i ++) 45 { 46 scanf("%d",&m); 47 f = 0; 48 for(j = 0;j < s3;j ++) 49 { 50 if(k[0] + c[j] <= m && k[t - 1] + c[j] >= m) 51 { 52 l = 0; 53 r = t - 1; 54 while(l <= r) 55 { 56 mid = (l + r)/2; 57 if(k[mid] + c[j] < m) 58 { 59 l = mid + 1; 60 } 61 else if(k[mid] + c[j] > m) 62 { 63 r = mid - 1; 64 } 65 else 66 { 67 f = 1; 68 break; 69 } 70 } 71 if(f) 72 break; 73 } 74 if(f) 75 break; 76 } 77 if(f) 78 printf("YES\n"); 79 else 80 printf("NO\n"); 81 } 82 } 83 return 0; 84 }