hdoj-2141-Can you find it?

来源:互联网 发布:医药魔方销售数据库 编辑:程序博客网 时间:2024/06/10 06:38

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
 


题意很容易理解,很水的一道模拟题,给你L个数A,N个数B,M个数C,然后一个X,求Ai+Bj+Ck = X,我拿到题的第一瞬间想到暴力,可是O(N3),1<=L, N, M<=500, 1<=S<=1000.是肯定会爆了,于是把第一个数组合第二个数组的所有组合线求出来,再将第三个数组排序,然后二分的去找,就ac了~~~


#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;int num[250005];bool tree(int a[],int x,int y){    bool f=false;    int lf,rig,mid;    lf=0;    rig=x-1;    mid=(lf+rig)/2;    //printf("1\n");    while(lf<=rig)    {        //printf("1\n");        if(a[mid]>y)            {                rig=mid-1;            }            else if(a[mid]<y)            {                lf=mid+1;            }        else{            f=true;            break;        }        mid=(lf+rig)/2;    }    return f;}int main(){    int ll[505],mm[505],nn[505];    int l,n,m,s;    int Cas=0;    bool flag;    while(scanf("%d%d%d",&l,&n,&m)!=EOF)    {        memset(ll,0,sizeof(ll));        memset(nn,0,sizeof(nn));        memset(mm,0,sizeof(mm));        for(int i=0;i<l;i++)            scanf("%d",&ll[i]);        for(int i=0;i<n;i++)            scanf("%d",&nn[i]);        for(int i=0;i<m;i++)            scanf("%d",&mm[i]);        int k=0;        for(int i=0;i<l;i++)            for(int j=0;j<n;j++)        {            num[k++]=ll[i]+nn[j];        }        sort(num,num+k);            Cas++;            printf("Case %d:\n",Cas);        scanf("%d",&s);        while(s--)        {            flag=0;            int x;            scanf("%d",&x);            for(int i=0;i<m;i++)            {                if(tree(num,k,x-mm[i]))                    {                        flag=1;                        break;                    }            }            if(flag) printf("YES\n");            else printf("NO\n");        }    }    return 0;}


0 0
原创粉丝点击