Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 24458 Accepted Submission(s): 6192
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
题目大意就是给你三个数组,然后再给一个数X,在这三个数组中分别找出一个数,是否存在使得Ai+Bj+Ck = X的数。
Code:
<span style="font-size:18px;">
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
bool binary_search1(__int64 D[],__int64 key,int len,__int64 temp)
{
int begin=0;
int end=len-1;
int mid;
while(begin<=end)
{
mid=((end-begin)>>1)+begin;
if(temp+D[mid]==key)
return true;
else if(temp+D[mid]<key)
begin=mid+1;
else
end=mid-1;
}
return false;
}
int main()
{
int cas=1,L,M,N;
__int64 A[505],B[505],C[505],D[505*505];//D数组的大小为n*n
while(~scanf("%d%d%d",&L,&N,&M))
{
for(int i=0;i<L;i++)
scanf("%I64d",&A[i]);
for(int i=0;i<N;i++)
scanf("%I64d",&B[i]);
for(int i=0;i<M;i++)
scanf("%I64d",&C[i]);
int k=0;
for(int i=0;i<N;i++){//用一个新的数组去保存B数组+C数组的值
for(int j=0;j<M;j++){
D[k++]=B[i]+C[j];
}
}
sort(D,D+k);//排序
int s;
scanf("%d",&s);
printf("Case %d:\n",cas++);
while(s--)
{
__int64 key;
int flag=0;
scanf("%I64d",&key);
for(int i=0;i<L;i++)//枚举A数组中的每一个数
{
if(binary_search1(D,key,k,A[i]))//然后再D数组中进行二分查找
{
printf("YES\n");
flag=1;
break;
}
}
if(!flag)
printf("NO\n");
}
}
return 0;
}</span>