首页 > 代码库 > hdu-2141
hdu-2141
Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 29184 Accepted Submission(s): 7286Problem DescriptionGive 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.
InputThere 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.
OutputFor 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 Input3 3 31 2 31 2 31 2 331410
Sample OutputCase 1:NOYESNO
题意:给出三个数列ABC,从ABC各取一个整数求是否能够等于X,是则输出YES,否NO。
三组500直接查找会超时
可将A+B+C=X转换为A+B=X-C
之后即可将等式两边看做两个整体进行二分查找。
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int a[510],b[510],c[510]; 5 int num[250010]; 6 7 int bin(int q[],int x,int y){ 8 int left=0,right=x,mid; 9 while(left<=right){10 mid=(left+right)/2;11 if(q[mid]==y)12 return 1;13 if(q[mid]>y)14 right=mid-1;15 else16 left=mid+1;17 }18 return 0;19 }20 21 int main(){22 int l,n,m,s,ans=0;23 while(cin>>l>>n>>m){24 ans++;25 for(int i=0;i<l;i++){26 cin>>a[i];27 }28 for(int i=0;i<n;i++){29 cin>>b[i];30 }31 for(int i=0;i<m;i++){32 cin>>c[i];33 }34 cin>>s;35 int x,cnt=0;36 37 for(int i=0;i<l;i++){38 for(int j=0;j<n;j++){39 num[cnt++]=a[i]+b[j];40 }41 }42 sort(num,num+cnt);43 printf("Case %d:\n",ans);44 while(s--){45 cin>>x;46 int flag=0;47 for(int i=0;i<m;i++){48 int temp=x-c[i];49 if(bin(num,cnt-1,temp)){50 cout<<"YES"<<endl;51 flag=1;52 break;53 }54 }55 if(!flag)56 cout<<"NO"<<endl;57 }58 }59 return 0;60 }
hdu-2141
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。