hdu2141 Can you find it? (二分查找)
2015-12-12 21:36
246 查看
Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 19334 Accepted Submission(s): 4860
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
Author
wangye
Source
HDU 2007-11 Programming Contest
Recommend
威士忌 | We have carefully selected several similar problems for you: 2289 1597 1551 2298 1399
解析:我们先预处理出a数列与b数列能够形成的所有数,存储在q数组当中,然后对q数组进行排序。
对于每一个X,我们枚举c[i],然后在q数组中二分查找是否存在X-c[i],若存在,则“YES”,否则“NO”。
代码:
#include<cstdio> #include<algorithm> using namespace std; const int maxn=5e2; int a[maxn+10],b[maxn+10],c[maxn+10]; int q[maxn*maxn+10]; bool find(int x) { int l=1,r=q[0],mid; while(l<=r) { mid=(l+r)>>1; if(q[mid]==x)return 1; if(q[mid]<x)l=mid+1; else r=mid-1; } return 0; } int main() { freopen("1.in","r",stdin); int sum=0,i,j,k,s,x; while(scanf("%d%d%d",&a[0],&b[0],&c[0])==3) { printf("Case %d:\n",++sum); for(i=1;i<=a[0];i++)scanf("%d",&a[i]); for(i=1;i<=b[0];i++)scanf("%d",&b[i]); for(i=1;i<=c[0];i++)scanf("%d",&c[i]); for(q[0]=0,i=1;i<=a[0];i++) for(j=1;j<=b[0];j++)q[++q[0]]=a[i]+b[j]; sort(q+1,q+1+q[0]); k=q[0],q[0]=1; for(i=2;i<=k;i++)if(q[i]!=q[i-1])q[++q[0]]=q[i]; k=c[0],c[0]=1; for(i=2;i<=k;i++)if(c[i]!=c[i-1])c[++c[0]]=c[i]; scanf("%d",&s); for(i=1;i<=s;i++) { scanf("%d",&x); for(j=1;j<=c[0];j++)if(find(x-c[j]))break; printf("%s\n",(j<=c[0])?"YES":"NO"); } } return 0; }
相关文章推荐
- 黑马程序员——java多线程
- Python学习(一)变量
- hadoop2.x上的Hive-1.x 安装
- GTW likes gt(BC 模拟 or 优先队列)
- 51单片机串口通信的帧数据接收
- Objc运行时读取和写入plist文件遇到的问题
- 学习progressDialog
- Objc运行时读取和写入plist文件遇到的问题
- Objc运行时读取和写入plist文件遇到的问题
- powerdesigner奇淫技
- CSS.DIV网页样式与布局学习总结
- 单机搭建基于Hadoop的Spark环境
- 输入两数相乘-经典递归
- cftool的应用
- 循环相关函数range、enumerate、zip
- 论学习的重要性
- POJ-3100
- "compileDebugJavaWithJavac"错误解决
- Cocos2D-Android-1之源码详解:16.ParticleTest
- 适配器模式(Adapter)和外观模式(Facade)