hdoj-2141-Can you find it?(二分查找)
1 /* 2 Name:HDU-2141-Can you find it? 3 Copyright: 4 Author: 5 Date: 2018/4/12 17:15:46 6 Description: 7 暴力,复杂度n^3,不存在的 8 将公式变换为A-B=X-C 9 枚举A-B,复杂度n^2 10 二分查找复杂度O(log2n),A-B是否存在A-B==X-C 11 */ 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 const int MAXN = 1e6 + 10; 16 int a[MAXN], b[MAXN], c[MAXN], ab[MAXN], x[MAXN]; 17 int main() 18 { 19 // freopen("in.txt", "r", stdin); 20 21 int l, n, m; 22 int cas = 0; 23 while (cin>>l>>n>>m) { 24 cout<<"Case "<<++cas<<":"<<endl; 25 for (int i=0; i<l; i++) { 26 cin>>a[i]; 27 } 28 for (int i=0; i<n; i++) { 29 cin>>b[i]; 30 } 31 int k = 0; 32 for (int i=0; i<l; i++) { 33 for(int j=0; j<n; j++) { 34 ab[k++] = a[i] + b[j]; 35 } 36 } 37 k--; 38 sort(ab, ab+k); 39 for (int i=0; i<m; i++) { 40 cin>>c[i]; 41 } 42 int s; 43 cin>>s; 44 for (int i=0; i<s; ++i) { 45 cin>>x[i]; 46 } 47 for (int i=0; i<s; i++) { 48 int flag = 0; 49 for (int j=0; j<m; ++j) { 50 int tmp = x[i] - c[j]; 51 if (binary_search(ab, ab+k, tmp) == true) { 52 flag = 1; 53 break; 54 } 55 } 56 if (flag) cout<<"YES"<<endl; 57 else cout<<"NO"<<endl; 58 } 59 } 60 return 0; 61 }