1 /* 2 题意:给出一个数,问是否有ai + bj + ck == x 3 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 5e2 + 10; 12 const int INF = 0x3f3f3f3f; 13 ll a[MAXN], b[MAXN], c[MAXN]; 14 ll sum[MAXN*MAXN]; 15 int tot; 16 17 bool my_binary_search(int l, int r, ll k) { 18 while (l < r) { 19 int mid = (l + r) >> 1; 20 if (sum[mid] == k) return true; 21 else if (sum[mid] > k) r = mid; 22 else l = mid + 1; 23 } 24 return false; 25 } 26 27 int main(void) { //HDOJ 2141 Can you find it? 28 //freopen ("HDOJ_2141.in", "r", stdin); 29 30 int l, n, m, s, cas = 0; 31 while (scanf ("%d%d%d", &l, &n, &m) == 3) { 32 for (int i=1; i<=l; ++i) scanf ("%I64d", &a[i]); 33 for (int i=1; i<=n; ++i) scanf ("%I64d", &b[i]); 34 for (int i=1; i<=m; ++i) scanf ("%I64d", &c[i]); 35 scanf ("%d", &s); 36 37 printf ("Case %d:\n", ++cas); 38 sort (a+1, a+1+l); sort (b+1, b+1+n); sort (c+1, c+1+m); 39 tot = 0; 40 for (int i=1; i<=l; ++i) { 41 for (int j=1; j<=n; ++j) { 42 sum[++tot] = a[i] + b[j]; 43 } 44 } 45 sort (sum+1, sum+1+tot); 46 47 ll mn = a[1] + b[1] + c[1], mx = a[l] + b[n] + c[m]; 48 while (s--) { 49 ll q; scanf ("%I64d", &q); 50 if (q < mn || q > mx) { 51 puts ("NO"); continue; 52 } 53 bool flag = false; 54 for (int i=1; i<=m; ++i) { 55 int p = lower_bound (sum+1, sum+1+tot, q - c[i]) - sum; 56 if (p < 1 || p > tot) continue; 57 if (sum[p] + c[i] == q) { 58 flag = true; puts ("YES"); break; 59 } 60 //if (my_binary_search (1, tot, q - c[i])) { 61 //flag = true; puts ("YES"); break; 62 //} 63 } 64 if (!flag) { 65 puts ("NO"); 66 } 67 } 68 } 69 70 return 0; 71 }
时间: 2024-10-22 13:48:01