HDU 2141(二分查找,binary_search(ab,ab+k,y))

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

#include<bits/stdc++.h>
#define maxn 510
using namespace std;
int a[maxn],b[maxn],c[maxn],ab[maxn*maxn];
int L,N,M,k;

/*bool judge(int x)
{
    int left=0,right=k;
    while(left<right)
    {
        int mid=left+(right-left)/2;
        if(ab[mid]<x) left=mid+1;
        else if(ab[mid]>x) right=mid;
        else return true;
    }
    return false;
}*/

int main()
{
    int cas=1;
    while(~scanf("%d%d%d",&L,&N,&M))
    {
        printf("Case %d:\n",cas++);
        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]);
        k=0;
        for(int i=0; i<L; i++)
            for(int j=0; j<N; j++)
                ab[k++]=a[i]+b[j];
        sort(ab,ab+k);
        int s,x;
        scanf("%d",&s);
        while(s--)
        {
            int flag=0;
            scanf("%d",&x);
            for(int j=0; j<M; j++)
            {
                int y=x-c[j];
                if(binary_search(ab,ab+k,y))
                {
                    flag=1;
                    break;
                }
            }
            if(flag) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beposit/article/details/80631345