现在的位置: 首页 > 综合 > 正文

hdu2141Can you find it?(二分查找)

2018年02月22日 ⁄ 综合 ⁄ 共 1542字 ⁄ 字号 评论关闭
Problem 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 3 1 2 3 1 2 3 1 2 3 3 1 4 10

Sample Output
Case 1: NO YES NO
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 4000000
int len[4],n;
__int64 ss[N],num[4][1005],x;
int cmp(int a,int b){return a<b;}
void dfs(__int64 sum,int v)
{
    if(v==2)
    {
        ss[n++]=sum; return;
    }
    for(int i=0;i<len[v];i++)
        dfs(sum+num[v][i],v+1);
}
int two(__int64 sum)
{
    int l,mid,r;
    l=0;r=n-1;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(ss[mid]==sum) break;
        if(ss[mid]<sum) l=mid+1;
        if(ss[mid]>sum) r=mid-1;
    }
    if(l<=r)return 1;
    return 0;
}
int main()
{
    int s,t=0,flog;
    while(scanf("%d%d%d",&len[0],&len[1],&len[2])>0)
    {
        n=0;
        for(int i=0;i<3;i++)
        for(int j=0;j<len[i];j++)
        scanf("%I64d",&num[i][j]);
        dfs(0,0);
        sort(ss,ss+n,cmp);
        scanf("%d",&s);
        printf("Case %d:\n",++t);
        while(s--)
        {
            scanf("%I64d",&x);
            flog=0;
            for(int i=0;i<len[2];i++)
            if(two(x-num[2][i]))
            {
                flog=1;break;
            }
            printf("%s\n",flog?"YES":"NO");
        }
    }
}

抱歉!评论已关闭.