hdu2141 Can you find it? (二分)

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
题意:给你L个A,N个B,M个C以及S个X,问对于每一个x,能否从A,B,C中各找出一个值,使得A+B+C=X.
思路:这是一道二分题,可以把A+B+C=X看做A+B=X-C,那么我们先把A+B并成一个集合Z,然后枚举X-C的值,二分寻找Z中是否有这个值。本来挺简单的二分,自己做的时候一直考虑二分C数组,时间复杂度怎么算都超了= .= 。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define eps 1e-15
#define maxn 506
ll a[maxn],b[maxn],c[maxn],d[2*maxn],e[maxn];
ll bing[maxn*maxn];

int main()
{
    int n,m,i,j,cas=0,t,p;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        for(i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        for(i=1;i<=m;i++){
            scanf("%lld",&b[i]);
        }
        int tot=0;
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                tot++;
                bing[tot]=a[i]+b[j];
            }
        }
        sort(bing+1,bing+1+tot);
        for(i=1;i<=t;i++){
            scanf("%lld",&c[i]);
        }
        scanf("%d",&p);
        for(i=1;i<=p;i++){
            scanf("%lld",&e[i]);
        }
        cas++;
        printf("Case %d:
",cas);
        for(i=1;i<=p;i++){
            int flag=0;
            for(j=1;j<=t;j++){
                int wei=lower_bound(bing+1,bing+1+tot,e[i]-c[j])-bing;
                if(bing[wei]==e[i]-c[j]){
                    flag=1;break;
                }
            }
            if(flag)printf("YES
");
            else printf("NO
");
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/herumw/p/9464549.html