hdu 2141 Can you find it?

Can you find it?

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

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)

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
 
Author
wangye
 
Source
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2199 2899 2289 1597 1551 
 一开始的思路:
(logn)^3
对每个序列都二分
这一个序列下一次搜索左右区间依据下一个序列最终二分点与中点的关系确定
写写才发现二分一次只能二分一个变量
不能多个变量同时二分
那如何控制二分变量唯一呢?
最好想的是枚举A和B,二分C  时间n²*logn,会TLE
本题还有一点只询问是否存在,不需要输出具体解
所以可以统计A和B任意两个数的和的情况,构成一个l*n的D数组
然后枚举C,二分D
自己写的时候不过脑子,枚举D,二分C,然后T了
#include<cstdio>
#include<algorithm>
using namespace std;
int l,n,m,s,x,sum,t;
int ll,rr,mid;
int a[501],b[501],c[501],ab[501*501];
bool ok;
int main()
{
    while(scanf("%d%d%d",&l,&n,&m)!=EOF)
    {
        t++;
        printf("Case %d:\n",t);
        sum=0;
        for(int i=1;i<=l;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n;i++) 
        {
            scanf("%d",&b[i]);
            for(int j=1;j<=l;j++) ab[++sum]=b[i]+a[j];
        }
        for(int i=1;i<=m;i++) scanf("%d",&c[i]); 
        sort(ab+1,ab+sum+1);
        sort(c+1,c+n+1);
        scanf("%d",&s);
        while(s--)
        {
            scanf("%d",&x);ok=false;
            for(int i=1;i<=sum;i++)
            {
                ll=1,rr=m;
                while(ll<=rr)
                {
                    mid=ll+rr>>1;
                    if(ab[i]+c[mid]==x) {ok=true;break;}
                    if(ab[i]+c[mid]>x) rr=mid-1;
                    else ll=mid+1;
                }
                if(ok) break;
            }
            if(ok) printf("YES\n");
            else printf("NO\n");
        } 
    }
}

 

posted @ 2017-03-14 08:19  TRTTG  阅读(253)  评论(0编辑  收藏  举报