HDU - 2141 Can you find it?
题意:从序列A,B,C中分别选一个数,有无可能等于X。
分析:
1、枚举A中所有数,由此可知X-Ai。
2、统计记录Bi+Ci的所有可能性
3、在 这所有可能性里二分找X-Ai
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 500 + 10; const int MAXT = 250000 + 10; using namespace std; int sum[MAXT]; int a[MAXN]; int b[MAXN]; int c[MAXN]; int cnt; bool solve(int x){ int l = 0, r = cnt - 1; while(l <= r){ int mid = l + (r - l) / 2; if(sum[mid] == x) return true; else if(sum[mid] < x) l = mid + 1; else r = mid - 1; } return false; } int main(){ int L, M, N; int kase = 0; while(scanf("%d%d%d", &L, &M, &N) == 3){ memset(sum, 0, sizeof sum); memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c); for(int i = 0; i < L; ++i){ scanf("%d", &a[i]); } for(int i = 0; i < M; ++i){ scanf("%d", &b[i]); } for(int i = 0; i < N; ++i){ scanf("%d", &c[i]); } cnt = 0; for(int i = 0; i < M; ++i){ for(int j = 0; j < N; ++j){ sum[cnt++] = b[i] + c[j]; } } sort(sum, sum + cnt); int S; scanf("%d", &S); printf("Case %d:\n", ++kase); while(S--){ bool ok = false; int x; scanf("%d", &x); for(int i = 0; i < L; ++i){ int t = x - a[i]; if(solve(t)){ printf("YES\n"); ok = true; break; } } if(!ok) printf("NO\n"); } } return 0; }