can you find it?
来源:互联网 发布:高等教育知乎 编辑:程序博客网 时间:2024/05/01 17:02
最近感觉二分压力超级大,所以和妹子一起做了二分的题目,好水的题目啊,可是没办法谁叫我们太弱了呢,继续加油,我要变大牛,不要做菜鸟。
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 31 2 31 2 31 2 331410一看数据那么大,就想到二分了,首先把二个变成一个再二分时间复杂度为nlog(n*n)这样就不会超时了。二分算法就不说了,地球人都知道。代码:#include<iostream>#include<cmath>#include<algorithm>using namespace std;int a[505],b[505],c[505];int dp[250025];int main(){ int i,j,k,m,n,s,t,cas=0,p,flag; while(scanf("%d%d%d",&k,&m,&n)!=EOF) { for(i=0;i<k;i++) scanf("%d",&a[i]); for(i=0;i<m;i++) scanf("%d",&b[i]); for(i=0;i<n;i++) scanf("%d",&c[i]); p=0; for(i=0;i<k;i++) for(j=0;j<m;j++) dp[p++]=a[i]+b[j]; sort(dp,dp+p); printf("Case %d:\n",++cas); scanf("%d",&t); while(t--) { scanf("%d",&s); flag=0; for(i=0;i<n;i++) { int l=0,r=p-1,mid; while(l<=r) { mid=(l+r)/2; if(dp[mid]+c[i]==s){ flag=1;break;} else if(dp[mid]+c[i]<s) l=mid+1; else if(dp[mid]+c[i]>s) r=mid-1; } if(flag) break; } if(flag) printf("YES\n"); else printf("NO\n"); }} return 0;}
- can you find it?
- Can you find it?
- Can you find it?
- Can you find it?
- can you find it?
- Can you find it?
- can you find it?
- Can you find it?
- Can you find it?
- Can you find it?
- Can you find it?
- can you find it?
- Can you find it?
- Can you find it?
- Can you find it?
- HDU Can you find it?
- HDU2141 Can you find it?
- Can you find it?---二分
- Storm数据流模型的分析及讨论
- 登录之找回密码
- K2 blackpearl 系列:SmartObject
- EXTJS 备注
- Linux小Tips--持续update~
- can you find it?
- 链表
- .NET笔试题集(三)
- Qt窗口屏幕居中显示以及设置大小
- 游戏打击感
- 在 Linux 平台中调试 C/C++ 内存泄漏方法
- HDU 2473 并查集加速 good
- 4 Values whose Sum is 0
- android LBS的定位源码解析