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.
InputThere 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.
OutputFor 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 10Sample Output
Case 1: NO YES NO
by talk:不想说话,心好累,wa许久后的ac。题意就是说让数列A,B,C里的数相加如果等于x输出YES,否则输出NO。这里相加是任意的。比如例子中x=1,三个最小的数1+1+1=3,相加都不等于1,输出no.x=4,取1+1+2=4 输出yes。x=10,三个最大的数相加3+3+3=9 都不为10 输出no.按照我们的思路就是定义一个sum[500*500*500]的数组将所有元素相加的情况存入数组中,再判断sum[i]=x但这样明显会超时。那么我们不妨数列A,B所有元素相加的情况存入一个数组里sum[500*500],然后再判断sum[p]是否=x-c[j]。因为p最大是500*500,我们对p二分,进而提高效率。
#include<cstdio>
#include<iostream>
#include<algorithm>using namespace std;
int a[503],b[503],c[503];
long long sum[503*503];
int main()
{
int l,n,m,i,j,kase=0,s,p,x,t;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
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]);
p=0;
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
sum[p++]=a[i]+b[j];
}
}
sort(sum,sum+p);
scanf("%d",&s);
printf("Case %d:\n",++kase);
while(s--)
{
int flag;
scanf("%d",&x);
flag=0;
for(j=0;j<m;j++)
{
t=x-c[j];
int left,right,mid;
left=0;right=p-1;
while(right-left>=0)
{
mid=(left+right)/2;
if(sum[mid]>t)
right=mid-1;
else if(sum[mid]<t)
left=mid+1;
else
{
flag=1;
break;
}
}
if(flag)
break;
}
if(flag==1)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}