HDU:2141 Can you find it?(二分+组合)

来源:互联网 发布:北京赛车两期计划数据 编辑:程序博客网 时间:2024/06/06 20:59

Can you find it?

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


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

威士忌


题目大意:给你三个序列长度,下面跟上三个序列,接着跟上几次查询,问是否能在三个序列中分别找出一个元素,相加得到查询的值。

解题思路:先将前两个序列两两相加,得到一个新的序列,将新的序列排序,然后与第三个序列进行二分组合搜索。

代码如下:

#include <cstdio>#include <algorithm>using namespace std;int l,n,m;int a[510];int b[510];int c[510];int niu[250010];//新a、b序列组成的新序列 int main(){int o=1;while(scanf("%d%d%d",&l,&n,&m)!=EOF){for(int i=0;i<l;i++){scanf("%d",&a[i]);}for(int i=0;i<n;i++){scanf("%d",&b[i]);}for(int i=0;i<m;i++){scanf("%d",&c[i]);}int cnt=0;//作为新数组的下标,同时计数新数组元素个数 for(int i=0;i<l;i++)//将前两个序列组合 {for(int j=0;j<n;j++){niu[cnt++]=a[i]+b[j];}}sort(niu,niu+cnt);//排序,进行二分搜索必不可少的一步 int t;scanf("%d",&t);printf("Case %d:\n",o++);while(t--){int x;scanf("%d",&x);int flag=0;int mid;for(int i=0;i<m;i++)//将第三个序列的每个元素都与新序列进行二分搜索 {int l=0,r=cnt-1;while(l<=r){mid=(l+r)/2;if(niu[mid]+c[i]==x){printf("YES\n");flag=1;break;}else{if(niu[mid]+c[i]>x){r=mid-1;}else{l=mid+1;}}}if(flag==1)break;}if(flag==0){printf("NO\n");}}}}


0 0
原创粉丝点击