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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<utility>
typedef long long ll;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;
const double e=2.718281828459 ;
const double esp=1e-9;
using namespace std;
int l,n,m,s,k;
int a[501],b[501],c[501],ss;
ll sum[501*501];
int bsearch(int x)
{
int low=0,high=k-1;
while(low<=high)
{
int mid=(low+high)>>1;
if(sum[mid]==x)
{
return 1;
}
else if(sum[mid]>x) high=mid-1;
else low=mid+1;
}
return 0;
}
int main()
{
int cnt=0;
while(~scanf("%d%d%d",&l,&n,&m))
{
memset(sum,0,sizeof(sum));
++cnt;
for(int i=0; i<l; i++)
scanf("%d",&a[i]);
for(int i=0; i<n; i++)
scanf("%d",&b[i]);
for(int i=0; i<m; i++)
scanf("%d",&c[i]);
sort(c,c+m);
scanf("%d",&s);
k=0;
for(int i=0; i<l; i++)
{
for(int j=0; j<n; j++)
{
sum[k++]=a[i]+b[j];
}
}
sort(sum,sum+k);
k=unique(sum,sum+k)-sum;
printf("Case %d:\n",cnt);
for(int i=0; i<s; i++)
{
scanf("%d",&ss);
if((ss>sum[k-1]+c[m-1])||(ss<sum[0]+c[0]))
{
printf("NO\n");
continue;
}
int flag=0;
for(int j=0; j<m; j++)
{
int mm=ss-c[j];
if(bsearch(mm))
{
flag=1;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-25045.html