HDU 2141 Can you find it?

题目地址:点击打开链接

思路:貌似不能把所有的和全存下吧,数组太大了,只能二分搜索了

AC代码:

#include <stdio.h>
#include <stdlib.h>

int a[510],b[510],c[510],num[510*510],f;

int cmp(const void *_a,const void *_b)
{
    return *(int*)_a - *(int*)_b;
}

int binsearch(int a)
{
    int left,right,mid;
    left = 0;
    right = f-1;
    while(left <= right)//注意是小于等于,如从123456中查找6,没等于不行
    {
        mid = (left + right) / 2;
        if(a == num[mid])
            return 1;
        if(a > num[mid])
            left = mid + 1;
        else
            right = mid - 1;
    }
    return 0;
}

int main()
{
    int l,m,n;
    int i,j,s,x,sum = 1;
    while(scanf("%d%d%d",&l,&m,&n) != EOF)
    {
        f = 0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0; i<m; i++)
        {
            scanf("%d",&b[i]);
        }
        for(i=0; i<l; i++)
        {
            scanf("%d",&c[i]);
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                num[f++] = a[i] + b[j];
            }
        }
        qsort(num,f,sizeof(int),cmp);
        qsort(c,l,sizeof(int),cmp);
        scanf("%d",&s);
        printf("Case %d:\n",sum++);
        while(s--)
        {
            scanf("%d",&x);
            for(i=0; i<n; i++)
            {
                if(binsearch(x-c[i]))
                {
                    break;
                }
            }
            if(i < n)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


你可能感兴趣的:(HDU 2141 Can you find it?)