HDOJ 2141 Can you find it?
2016-07-27 21:32
302 查看
Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 23857 Accepted Submission(s): 6049
[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
三个for肯定是要超时的,(别问我为什么,因为题出在二分专题)。所以可以先让第一第二组结合在对第三组二分。
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<iostream> using namespace std; int a[505],b[505],c[505]; int sum[505*505]; int l,n,m,k; int flag; void binary(int x) { int left,right,mid; left=0,right=k-1; while(left<=right) { mid=(left+right)/2; if(sum[mid]>x) right=mid-1; else if(sum[mid]<x) left=mid+1; else {flag=1;return ;}//retrn ;用来跳出子函数,在这里不能少。 } return ; } int main() { int i,j,q,x,cnt=1; while(scanf("%d%d%d",&l,&m,&n)!=EOF) { for(i=0;i<l;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) scanf("%d",&b[i]); for(i=0;i<m;i++) scanf("%d",&c[i]); k=0; for(i=0;i<l;i++) for(j=0;j<n;j++) { sum[k++]=a[i]+b[j]; } sort(sum,sum+k); scanf("%d",&q); printf("Case %d:\n",cnt++); while(q--) { scanf("%d",&x); flag=0; for(i=0;i<m;i++) { binary(x-c[i]); if(flag){ printf("YES\n"); break; } } if(!flag) printf("NO\n"); } } return 0; }
相关文章推荐
- WebRTC
- 在Ubuntu操作系统中添加环境变量
- css 居中总结
- 深入理解HTTP Session
- hdu 3652 B-number 数位dp
- php设计模式之观察者模式
- Android打电话,发短信【JAVA】
- WebApi授权拦截——重写AuthorizeAttribute
- MDK注释时用@使单词高亮
- MDK注释时用@使单词高亮
- HDU 1873
- HDU 1551 Cable master / UVALive 2452 Cable master(切电缆,二分)
- 2016夏季练习——最小树形图
- ECMAScript总结
- BFS-Oil Deposits
- DHCP Set Hostname
- 微信开发入门
- php设计模式之单例模式
- 记某次codeforces考试 A. Levko and Array Recovery
- js 的 深拷贝 和 浅拷贝