您的位置:首页 > 其它

hdu2141_Can you find it?

2015-07-23 08:42 357 查看
Can you find it?
Time Limit:3000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status

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

先合并两个再进行二分
#include <stdio.h>
#include <algorithm>
#include <string.h>
const int maxn=501;
using namespace std;

int a[maxn],b[maxn],c[maxn],s,tab[maxn*maxn];

bool bs(int cn,int key)
{
int low=0,high=cn-1,mid;
while(low<=high)
{
mid=low+(high-low)/2;
if(tab[mid]==key) return true;
else if(tab[mid]>key) high=mid-1;
else low=mid+1;
}
return false;
}

int main()
{
int l,m,n,cnt=0;
while(scanf("%d%d%d",&l,&m,&n)!=EOF)
{
int x;int cn=0;
for(int i=0;i<l;i++) scanf("%d",&a[i]);
for(int j=0;j<m;j++) scanf("%d",&b[j]);
for(int k=0;k<n;k++) scanf("%d",&c[k]);
for(int i=0;i<l;i++)
{
for(int j=0;j<m;j++)
{
tab[cn++]=a[i]+b[j];
}
}
sort(tab,tab+cn);
sort(c,c+n);
scanf("%d",&s);
printf("Case %d:\n",++cnt);

for(int p=0;p<s;p++)
{
bool pt=false;
scanf("%d",&x);
for(int i=0;i<n;i++)
{
if(bs(cn,x-c[i]))
{
pt=true;
printf("YES\n");
break;
}
}
if(!pt)printf("NO\n");
}

memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(tab,0,sizeof(tab));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: