hdu-2141 Can you find it?---暴力+二分
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2141
题目大意:
给ABC三个数组,给一个X,求是否存在Ai+Bj+Ck = X
思路:
等式转化成Ai+Bj = X-Ck
这样预处理出Ai+Bj的所有数字,然后循环k,二分查找X-Ck是否存在。
首先用set超内存,然后用数组结果一直WA,二分的时候少了个”=“
这真是醉了。
果然深夜不适合刷题
1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdio> 7 #include<set> 8 using namespace std; 9 typedef pair<int, int> Pair; 10 typedef long long ll; 11 const int INF = 0x3f3f3f3f; 12 int T, n, m1, m2, m3, cases; 13 ll a[500], b[500], c[500]; 14 ll cnt[250010]; 15 int tot; 16 bool Find(ll x) 17 { 18 int l = 0, r = tot-1; 19 while(l <= r) 20 { 21 int mid = (l + r) / 2; 22 if(cnt[mid] == x)return true; 23 else if(cnt[mid] < x)l = mid + 1; 24 else r = mid - 1; 25 } 26 return false; 27 } 28 int main() 29 { 30 while(scanf("%d%d%d", &m1, &m2, &m3) != EOF) 31 { 32 memset(cnt, 0, sizeof(cnt)); 33 tot = 0; 34 for(int i = 0; i < m1; i++)cin >> a[i]; 35 for(int i = 0; i < m2; i++)cin >> b[i]; 36 for(int i = 0; i < m3; i++)cin >> c[i]; 37 for(int i = 0; i < m1; i++) 38 for(int j = 0; j < m2; j++)cnt[tot++] = (a[i] + b[j]); 39 sort(cnt, cnt + tot); 40 sort(c, c + m3); 41 scanf("%d", &n); 42 printf("Case %d:\n", ++cases); 43 while(n--) 44 { 45 ll x; 46 cin >> x; 47 bool flag = 0; 48 for(int i = 0; i < m3; i++) 49 { 50 if(Find(x - c[i])) 51 { 52 flag = 1; 53 break; 54 } 55 } 56 if(flag){printf("YES\n");continue;} 57 printf("NO\n"); 58 } 59 } 60 }
越努力,越幸运