首页 > 代码库 > hdu 2141 (二分)

hdu 2141 (二分)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141

 

Can you find it?

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


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
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
这题写的不爽,不写题解了,看代码吧
 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6  7 using namespace std; 8  9 long long sum[250005];10 int str1[505],str2[505],str3[505];11 12 int Table(int l,int n,int m)13 {14 15     int cas=0;16     for(int i=0; i<l ;i++)17     {18 19         for(int j=0; j<n; j++)20         {21 22             sum[cas++]=str1[i]+str2[j];23         }24     }25     return cas;26 }27 28 bool BSearch(long long sum[],int k,int cas)29 {30 31     int left=0,right=cas-1;32     while(left<=right)33     {34 35         int mid=(left+right)>>1;36         if(sum[mid] == k) return true;37         else if(k < sum[mid]) right=mid-1;38         else left = mid+1;39     }40     return false;41 }42 43 int main()44 {45 46     int l,m,n,i,j;47     int ss=1;48     while(scanf("%d%d%d",&l,&n,&m)!=EOF)49     {50 51         memset(str1,0,sizeof(str1));52         memset(str2,0,sizeof(str2));53         memset(str3,0,sizeof(str3));54 55         for(i=0; i<l; i++)56             scanf("%d",&str1[i]);57         for(j=0; j<n; j++)58             scanf("%d",&str2[j]);59         for(i=0; i<m; i++)60             scanf("%d",&str3[i]);61         int k,tmp;62         scanf("%d",&k);63         int cas=Table(l,n,m);64         sort(sum,sum+cas);65         printf("Case %d:\n",ss++);66         while(k--)67         {68                 scanf("%d",&tmp);69                 for(i=0; i<m; i++)70                 {71 72                     if(BSearch(sum,tmp-str3[i],cas))73                     {74 75                         printf("YES\n");76                          break;77                     }78                 }79                 if(i>=m)printf("NO\n");80         }81         82     }83     return 0;84 }
View Code

 

 

hdu 2141 (二分)