Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 17505 Accepted Submission(s): 4426
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
C语言程序代码
给三个数L,N,M,接着给三组数,第一组是L个数,第二组是N个数
第三组是M个数,从这三组中各拿一个数相加。然后给一个数T,表示
接下来要输入T个数,判断它是否等于拿出的三个数的和。如是,输出
YES,否则输出NO。
解题思路::
用二分法做,先将前两组数两两相加把所有情况存放到一个数组中,对其进行排序
然后与要比较的数取差,最后与第三组比较,如相等则跳出,否则继续比较
(用二分法节省时间)
*/
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[510],b[510],c[510],d[250010];
int main(){
int L,N,M,i,j,k,n,m,l,t,s,x,y,flag;
s=0;
while(~scanf("%d %d %d",&L,&N,&M))
{
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]);
sort(c,c+M);
m=0;
for(j=0;j<L;j++)
for(k=0;k<N;k++)
d[m++]=a[j]+b[k];
sort(d,d+m);
scanf("%d",&t);
printf("Case %d:\n",++s);
for(i=0;i<t;i++)
{
scanf("%d",&x);
flag=0;
for(k=0;k<M;k++)
{
y=x-c[k];
int le,r,mid;
le=0;r=m-1;
while(le<=r)
{
mid=(le+r)/2;
if(d[mid]==y)
{
flag=1;
break;
}
if(d[mid]<y)
le=mid+1;
else
r=mid-1;
}
if(flag==1)
break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}