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
10
Sample Output
Case 1:
NO
YES
NO题目大意:输入3个数组,3个数组中的元素相加,判断是否能得到x;
题目的输入输出有点恶心人,,,写的时候弄的我晕 ,,哇了好几次
思路 :一开始想到的是暴力枚举,,但是肯定会TLE 看了一下大佬们的博客,,用二分法方便一点 就是让A+B构成一个新的数组sum,x-c[i]构成一个新的数组cc,然后在sum中查找是否存在cc中的元素有的话返回YES否则返回NO
AC代码:(本人不太擅长二分所以代码质量不高)
#include<iostream>
#include<algorithm>
using namespace std;
int a[];
int b[];
int c[];
int sum[*+];
int pos;
int judge(int x){ if(x<sum[]||x>sum[pos-]) return ; int low=,high=pos-;
while(low<=high){
int mid=(high+low)/;
// cout<<mid<<endl;
if(sum[mid]>x){
high=mid-;
}
else if(sum[mid]<x) low=mid+;
else {
return ;
}
}
return ;
}int main()
{
int l,m,n,ll=;
while(cin>>l>>m>>n)
{
ll++;
for(int i=;i<l;i++)
cin>>a[i];
for(int j=;j<m;j++)
cin>>b[j];
for(int k=;k<n;k++)
cin>>c[k]; pos=;
for(int i=;i<l;i++)
for(int j=;j<m;j++){
sum[pos++]=a[i]+b[j];
}
sort(sum,sum+pos); int xx;
cin>>xx; printf("Case %d:\n",ll); for(int i=;i<=xx;i++){
int x,flag=;
cin>>x;
for(int i=;i<n;i++){
if(judge(x-c[i])){
flag=;
break;
}
} if(flag)
printf("YES\n");
else printf("NO\n"); }
}
return ;
}