Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others) Total Submission(s): 8575    Accepted Submission(s): 2241

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
 
题意:给你三组数据,每组有L,N,M个数,每组取一个相加,问在他们的和数组里有没有x这个数?;
 
思路:数组+二分。。。。。
 
 
详见代码:
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std; int cmp(int x,int y)
{
return x<y;
}
__int64 num[];
int main()
{
int S,ans,i,j,k,temp,t=;
int L,N,M,a[],b[],c[],p; int left,right,middle;
while(scanf("%d%d%d",&L,&N,&M)!=EOF)
{
// memset(num,0,sizeof(num));
for(i=;i<L;i++)
{
scanf("%d",&a[i]);
}
// sort(a,a+L,cmp);
for(i=;i<N;i++)
{
scanf("%d",&b[i]);
}
// sort(b,b+N,cmp);
for(i=;i<M;i++)
{
scanf("%d",&c[i]);
}
// sort(c,c+M,cmp);
ans=;
for(i=;i<L;i++)
for(j=;j<N;j++)
{
num[ans++]=a[i]+b[j];
}
sort(num,num+ans,cmp);
printf("Case %d:\n",t++);
scanf("%d",&S);
while(S--)//for(i=0;i<S;i++)写成这样,,,那怪超时。。。顿时郁闷了!
{
temp=;
scanf("%d",&p);
for(i=;i<M;i++)
{
if(num[]<=p-c[i]&&p-c[i]<=num[ans-])
{
left=;
right=ans-; while(right-left>=)
{
middle=(left+right)/; if(num[middle]>p-c[i])
{
right=middle-;
}
else if(num[middle]<p-c[i])
{
left=middle+;
}
else
{temp=;break;}
} }
else
temp=;
if(temp)
break;
}
if(temp)
printf("YES\n");
else
printf("NO\n");
}
}
return ;
}
/*
3 3 3
1 2 3
1 2 3
1 2 3
11
4
1
10
9
8
7
6
5
3
2
11
*/
04-25 17:12
查看更多