Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 17556 Accepted Submission(s): 4439
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
//二分法, 查找的是有序数组的下标,
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int a[550], b[550], c[550]; __int64 d[250050]; 5 int main() 6 { 7 int l,m ,n, cas=1; 8 while(cin >> l >> m >> n) 9 { 10 11 int i, j, k, t=0; 12 for(i=0; i<l; i++) 13 cin >> a[i]; 14 for(i=0; i<m; i++) 15 cin >> b[i]; 16 for(i=0; i<n; i++) 17 cin >> c[i]; 18 for(i=0; i<l; i++) 19 for(j=0 ;j<m; j++) 20 d[t++] = a[i] + b[j]; 21 sort(d, d+t); 22 printf("Case %d: ", cas++); 23 int g, sum; 24 scanf("%d", &g); 25 while(g--) 26 { 27 cin >> sum; 28 int ok = 0; 29 for(i=0; i<n; i++) 30 { 31 if(d[0] + c[i] <= sum && d[t-1] + c[i] >=sum) // 32 { 33 int l=0, r=t-1, mid; 34 while(r-l >= 0) 35 { 36 mid = (l+r)/2; 37 if(c[i] + d[mid] > sum) r = mid -1; 38 else if(d[mid] + c[i] < sum) l = mid + 1; 39 else 40 { ok = 1; break;} 41 } 42 if(ok) break; 43 } 44 } 45 if(ok) 46 printf("YES "); 47 else 48 printf("NO "); 49 } 50 } 51 return 0; 52 }
//感觉二分法做的确实有点懵。