HDOJ Can you find it? 2141(二分搜索)

来源:互联网 发布:mac安装包损坏 编辑:程序博客网 时间:2024/06/05 13:35

Can you find it?

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


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 31 2 31 2 31 2 331410
 

Sample Output
Case 1:NOYESNO
 

Author
wangye
 

Source
HDU 2007-11 Programming Contest
 

Recommend
威士忌   |   We have carefully selected several similar problems for you:  2199 2899 2289 1597 1551 

二分搜索。。。。上机课一水~

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<algorithm>#define INF 250000+10using namespace std;typedef __int64 LL;LL T[INF];bool search(LL *D,LL s,LL x,LL y){LL mid;while(x<=y){mid=(x+y)/2;if(D[mid]<s){x=mid+1;}else if(D[mid]>s){y=mid-1;}else if(D[mid]==s)return true;}return false;}int main(){int L,N,M;LL a[510],b[510],c[510];int x=1;while(scanf("%d%d%d",&L,&N,&M)!=EOF){memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));memset(T,0,sizeof(T));for(int i=0;i<L;i++)scanf("%I64d",&a[i]);for(int i=0;i<N;i++)scanf("%I64d",&b[i]);for(int i=0;i<M;i++)scanf("%I64d",&c[i]);LL tot=0;for(int i=0;i<L;i++)  for(int j=0;j<N;j++)  T[tot++]=a[i]+b[j];sort(T,T+tot);int S;printf("Case %d:\n",x++);scanf("%d",&S);while(S--){LL v=0;int t=0;scanf("%I64d",&v);for(int i=0;i<M;i++){LL p=v-c[i];if(search(T,p,0,tot)){t=1;break;}}if(t)printf("YES\n");else printf("NO\n");}}return 0;}


 
0 0