HDOJ-----2141二分
2016-07-27 21:23
423 查看
Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 23851 Accepted Submission(s): 6046
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
3 3 3 1 2 3 1 2 3 1 2 3 3 1 4 10
[align=left]Sample Output[/align]
Case 1: NO YES NO
就是给三组序列,L个A,N个B,M个C,下面给出一系列数X,问能不能从三组序列各取一个加在一起等于这个数,这个主要是超时,三重循环,每重500次,每个数要循环10的8次方,肯定会爆的(我试了一下,机房电脑嗡嗡响,吓得我赶紧关了),先把任意两个序列两两相加得出一个新的序列S,大概500*500大小,减少了500倍,虽然还是很大,但还算可以接受。然后sort一下,用给出让判断的数X循环减去剩下那个序列E中的元素,再用二分搜索这个差是否存在S序列中
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[555], b[555], c[555], q[250010]; int main(){ int L, M, N, S, x, t = 1, v, u; while(~scanf("%d%d%d", &L, &N, &M)){ for(int i = 0; i < L; i++){ scanf("%d", &a[i]); } v = 0; for(int i = 0; i < N; i++){ scanf("%d", &b[i]); for(int j = 0; j < L; j++){ q[v++] = a[j] + b[i]; } } for(int i = 0; i < M; i++){ scanf("%d", &c[i]); } sort(q, q + v); scanf("%d", &S); printf("Case %d:\n", t++); while(S--){ scanf("%d", &x); int cas = 0; for(int i = 0; i < M; i++){ int ans = x - c[i]; int e, s, m, ok = 0; e = 0; s = v-1; while(e <= s){ m = (e + s) >> 1; if(q[m] == ans){ ok = 1; break; } if(q[m] > ans){ s = m-1; } else{ e = m+1; } } if(ok){ printf("YES\n"); cas++; break; } } if(!cas){ printf("NO\n"); } } } return 0; }
相关文章推荐
- JAVA学习笔记Day02——基本语法
- 每日小结
- mysql查询性能优化
- 【Postgresql源码分析之四】同步复制源码分析--支持多个同步备机
- c语言指针操作
- poj1060
- 190. Reverse Bits
- 机房收费(五)之总结
- java 抓取 网页 数据
- Cookie/Session机制详解
- Volley报错!!!No address associated with hostname
- 17.3
- 100条使信号完整性问题最小化的通用设计原则(转)
- python 培训之Django
- 链接集合
- MVC设计模式
- Codeforces 612C
- css样式重置
- Linux文件管理及相关命令-1
- 使用Spring进行依赖注入