HDU--杭电--2141--Can you find it?--二分

来源:互联网 发布:用友网络股份有限公司 编辑:程序博客网 时间:2024/05/22 06:09

Can you find it?

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


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


题意就是给你三个数列,分别是a、b、c的,从中选取组合看能不能搞成a+b+c=X,能就yes不能就no
思路:三个都是最大可以能为500的数组,所以直接暴力会超时,这时就二分查找,还要加上一个小技巧,就是合并其中任意两个数组,也就是把所有a+b的和装起来,这样就成了ab+c的运算了
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;__int64 a[555],ab[255555],c[555];bool double_kill(__int64 l,__int64 r,__int64 s)//二分查找{    __int64 mid;    while(l<=r)    {        mid=(l+r)/2;        if(ab[mid]==s)return 1;        if(ab[mid]<s)l=mid+1;        else r=mid-1;    }    return 0;}int main (void){    __int64 i,j,k,l,n,m,cas=1,q,w,e;    while(scanf("%I64d%I64d%I64d",&q,&w,&e)!=EOF)    {        for(i=0;i<q;i++)        scanf("%I64d",&a[i]);        for(i=l=0;i<w;i++)        {            scanf("%I64d",&k);            for(j=0;j<q;j++)ab[l++]=a[j]+k;//把a+b存在ab数组里        }        for(i=0;i<e;i++)        scanf("%I64d",&c[i]);        sort(ab,ab+l);//排序,二分的需求        scanf("%I64d",&n);        printf("Case %I64d:\n",cas++);        while(n--&&scanf("%I64d",&m))        {            for(i=0;i<e;i++)            if(m>=ab[0]+c[i]&&m<=ab[l-1]+c[i]&&double_kill(0,l-1,m-c[i]))            break;            if(i==e)puts("NO");            else puts("YES");        }    }    return 0;}
  这里用__int64是因为最大是32位,而且还要进行运算, 32位+32位 在32位里面放不下,所以用64位了

原创粉丝点击