hdoj 2141 Can you find it? 【二分 好题】 【求STL set 怎么破】
2015-07-30 16:24
281 查看
Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 16926 Accepted Submission(s): 4305
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
题意就不说了吧。。。
思路:把两个数组压缩成一个新数组T,枚举另外一个数组元素Y。对于要验证的结果X:则问题就变成了在数组T里面查找是否存在元素X - Y。
AC代码:561ms 在压缩新数组的时候,可以标记下某个元素是否存在,若存在不再进数组。在遍历另外一个数组元素时也可以标记下,这样应该能稍稍优化下吧。有兴趣的可以写下。
注意后面两个代码是错误的!!!
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 500+1 using namespace std; int a[MAXN], b[MAXN], c[MAXN]; int d[250000+100]; int main() { int L, N, M, S, X; int k = 1; int top; bool flag; while(scanf("%d%d%d", &L, &N, &M) != EOF) { for(int i = 0; i < L; i++) scanf("%d", &a[i]); for(int i = 0; i < N; i++) scanf("%d", &b[i]); for(int i = 0; i < M; i++) scanf("%d", &c[i]); //sort(a, a+L); //sort(b, b+N); top = 0; for(int i = 0; i < L; i++) { for(int j = 0; j < N; j++) d[top++] = a[i] + b[j]; } sort(d, d+top); scanf("%d", &S); printf("Case %d:\n", k++); while(S--) { scanf("%d", &X); flag = false; for(int i = 0; i < M; i++) { int ans = X - c[i];//我们要搜索的结果 int left = 0, right = top - 1, mid; while(right >= left) { mid = (left + right) >> 1; if(d[mid] == ans) { flag = true; break; } else if(d[mid] > ans) right = mid - 1; else left = mid + 1; } if(flag) break; } if(!flag) printf("NO\n"); else printf("YES\n"); } } return 0; }
错误代码一:
考虑到可能会有重复的元素,想用set优化下
用set存储新数组元素一直MLE,无语。。。。
#include <cstdio> #include <cstring> #include <set> #include <algorithm> #define MAXN 500+1 using namespace std; int a[MAXN], b[MAXN], c[MAXN]; int main() { int L, N, M, S, X; int k = 1; int top; bool flag; while(scanf("%d%d%d", &L, &N, &M) != EOF) { for(int i = 0; i < L; i++) scanf("%d", &a[i]); for(int i = 0; i < N; i++) scanf("%d", &b[i]); for(int i = 0; i < M; i++) scanf("%d", &c[i]); set<int> d; for(int i = 0; i < L; i++) { for(int j = 0; j < N; j++) d.insert(a[i] + b[j]); } scanf("%d", &S); printf("Case %d:\n", k++); while(S--) { scanf("%d", &X); flag = false; for(int i = 0; i < M; i++) { int ans = X - c[i];//我们要搜索的结果 set<int>::iterator pos; pos = d.find(ans); if(pos != d.end()) { flag = true; break; } } if(!flag) printf("NO\n"); else printf("YES\n"); } } return 0; }
代码二:用set存储另外一个数组,结果TLE。。。
#include <cstdio> #include <cstring> #include <set> #include <algorithm> #define MAXN 500+1 using namespace std; int a[MAXN], b[MAXN], c[MAXN]; int e[2500000+10]; set<int> d; int main() { int L, N, M, S, X; int k = 1; int top; bool flag; while(scanf("%d%d%d", &L, &N, &M) != EOF) { d.clear(); for(int i = 0; i < L; i++) scanf("%d", &a[i]); for(int i = 0; i < N; i++) scanf("%d", &b[i]); for(int i = 0; i < M; i++) scanf("%d", &c[i]), d.insert(c[i]); top = 0; for(int i = 0; i < L; i++) { for(int j = 0; j < N; j++) e[top++] = a[i] + b[j]; } scanf("%d", &S); printf("Case %d:\n", k++); while(S--) { scanf("%d", &X); flag = false; for(int i = 0; i < top; i++) { int ans = X - e[i];//我们要搜索的结果 set<int>::iterator pos; pos = d.find(ans); if(pos != d.end()) { flag = true; break; } } if(!flag) printf("NO\n"); else printf("YES\n"); } } return 0; }
相关文章推荐
- 深入探讨 Java 类加载器
- Python3.4网页爬虫,提取图片
- EasyUI基础searchbox&progressbar(搜索框,进度条)
- 插入排序
- 使用window live writer
- [Jquery]某宝图片轮播(无缝、带左右切换按钮)
- http://xiejianglei163.blog.163.com/blog/static/1247276201441910377144/
- epoll实现分析
- 中文linux安装oracle界面乱码解决方案
- 定时任务会重复执行俩次的解决方法
- 定时任务会重复执行俩次的解决方法
- 定时任务会重复执行俩次的解决方法
- 源码推荐(6.16):高仿新浪微博,支付宝(高仿)源码
- HDU 1272 小希的迷宫 并查集
- 使Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示。
- 我要娶你做我的CoreData!
- github 中文教程
- 相邻div实现一个跟着另一个自适应高度示例代码
- 资源模块句柄的切换 .
- 判断DataGridView滚动条是否滚动到当前已加载的数据行底部