hdu 2141 Can you find it?

http://acm.hdu.edu.cn/showproblem.php?pid=2141

二分

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<map>

#include<vector>

#include<stack>

#include<set>

#include<map>

#include<queue>

#include<deque>

#include<algorithm>

#include<cmath>

#define LL long long

//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double eps=1e-9;

const int INF=0x3f3f3f3f;

const double FINF=1e9;

const int N=1005;

LL a[N],b[N],c[N];

LL k[N*N];

bool binsearch(LL w,int l,int r)

{

    while(l<=r)

    {

        int mid=(l+r)/2;

        if(k[mid]==w)

        return true;

        if(k[mid]<w)

        l=mid+1;

        else

        r=mid-1;

    }

    return false;

}

int main()

{

    //freopen("data.in","r",stdin);

    //freopen("data.out","w",stdout);

    int T=0;

    int A,B,C;

    while(scanf("%d %d %d",&A,&B,&C)!=EOF)

    {

        ++T;

        for(int i=0;i<A;++i)

        scanf("%I64d",&a[i]);

        for(int i=0;i<B;++i)

        scanf("%I64d",&b[i]);

        for(int i=0;i<C;++i)

        scanf("%I64d",&c[i]);

        int I=0;

        for(int i=0;i<A;++i)

        for(int j=0;j<B;++j)

        k[I++]=a[i]+b[j];

        sort(k,k+I);

        printf("Case %d:\n",T);

        int q;

        scanf("%d",&q);

        while(q--)

        {

            LL s;

            scanf("%I64d",&s);

            int l;

            for(l=0;l<C;++l)

            {

                if(binsearch(s-c[l],0,I-1))

                break;

            }

            if(l<C)

            printf("YES\n");

            else

            printf("NO\n");

        }

    }

    return 0;

}

  

你可能感兴趣的:(find)