hdu2141 二分搜索+数据处理

来源:互联网 发布:java四个月培训找工作 编辑:程序博客网 时间:2024/05/18 05:40
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

__int64 L,N,M,S,num=0;
__int64 A[505],B[505],C[505],AB[250005];
int judge(__int64 x)
{
    if(x>AB[L*N-1]+C[M-1]||x<AB[0]+C[0])
    {
        printf("NO\n");
        return 0;
    }
    else
    {
        __int64 temp;
        for(__int64 i=0; i<M; i++)
        {
            temp=x-C[i];
            __int64 s=0,e=L*N-1,mid;
            while(s<=e)
            {
                mid=(s+e)/2;
                if(AB[mid]==temp)
                {
                    printf("YES\n");
                    return 0;
                }
                else if(AB[mid]>temp)
                    e=mid-1;
                else
                    s=mid+1;
            }
        }
    }
    printf("NO\n");
    return 0;
}
int main ()
{
    while(scanf("%I64d%I64d%I64d",&L,&N,&M)!=EOF)
    {
        for(__int64 i=0; i<L; i++)
            scanf("%I64d",&A[i]);
        for(__int64 i=0; i<N; i++)
            scanf("%I64d",&B[i]);
        for(__int64 i=0; i<M; i++)
            scanf("%I64d",&C[i]);
        for(__int64 i=0,k=0; i<L; i++)
            for(__int64 j=0; j<N; j++)
                AB[k++]=A[i]+B[j];
        sort(C,C+M);
        sort(AB,AB+L*N);
        scanf("%I64d",&S);
        num++;
        printf("Case %I64d:\n",num);
        for(__int64 i=0; i<S; i++)
        {
            __int64 temp;
            scanf("%I64d",&temp);
            judge(temp);
        }
    }


    return 0;
}
0 0
原创粉丝点击