Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 42890 Accepted Submission(s): 10395
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
Author
wangye
Source
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int l[1005],n[1005],m[1005],c[250005]; 4 int ef(int l,int n,int m){ 5 int mid; 6 while(l<=n){ 7 mid=(l+n)/2; 8 if(c[mid]>m) 9 n=mid-1; 10 else if(c[mid]<m) 11 l=mid+1; 12 else 13 return -1; 14 } 15 return 1; 16 } 17 int main(){ 18 int a,b,s,x,p; 19 int i,j,h,num=0,len,ans; 20 while(~scanf("%d%d%d",&a,&b,&s)){ 21 for(i=0;i<a;i++) 22 scanf("%d",&l[i]); 23 for(i=0;i<b;i++) 24 scanf("%d",&n[i]); 25 for(i=0;i<s;i++) 26 scanf("%d",&m[i]); 27 h=0; 28 for(i=0;i<a;i++){ 29 for(j=0;j<b;j++) 30 c[h++]=l[i]+n[j]; 31 } 32 len=a*b; 33 sort(c,c+len); 34 scanf("%d",&p); 35 printf("Case %d: ",++num); 36 while(p--){ 37 scanf("%d",&x); 38 for(i=0;i<s;i++){ 39 ans=ef(1,len,x-m[i]); 40 if(ans==-1) 41 break; 42 } 43 if(ans==1) 44 printf("NO "); 45 else 46 printf("YES "); 47 } 48 } 49 return 0; 50 }