hdu 2141 Can you find it?(二分)

来源:互联网 发布:asp信息录入系统源码 编辑:程序博客网 时间:2024/05/01 11:30

Can you find it?

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


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


tips:利用unique进行查重,一定要先排序再进行查重。


三个数组两个数组之间二分的处理方式。


 #include<iostream> #include<algorithm> using namespace std;  __int64 a[503]; __int64 b[503]; __int64 c[503];   int len; __int64 sum[300001]; int _case; int la,lb,lc;  int bin(__int64 x) { int left=0; int right=len-1;  while(left<=right) { int mid=(left+right)/2;  if(x>sum[mid])left=mid+1; else if(x==sum[mid])return 1; else right=mid-1; }  return 0; }  int main() { while(cin>>la>>lb>>lc) { _case++; cout<<"Case "<<_case<<":"<<endl; len=0;  for(int i=0;i<la;i++) cin>>a[i]; for(int j=0;j<lb;j++) cin>>b[j]; for(int k=0;k<lc;k++) cin>>c[k];  sort(c,c+lc);for(int i=0;i<la;i++)for(int j=0;j<lb;j++){sum[len++]=a[i]+b[j];}sort(sum,sum+len);//使用unique函数进行去重,一定要先排序再去重  len=unique(sum,sum+len)-sum;   int x; cin>>x;  while(x--) { int flag=0;  __int64 ans; cin>>ans;  if(ans<c[0]+sum[0]||ans>c[lc-1]+sum[len-1]){cout<<"NO"<<endl;continue; }  for(int i=0;i<lc;i++) { if(bin(ans-c[i])) { flag=1; break; } }  if(flag)cout<<"YES"<<endl; else cout<<"NO"<<endl;     }  }  return 0; }



0 0
原创粉丝点击