HDU2141——二分——Can you find it?

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
 
/*
把两个加在一起,和第三个二分
换下顺序就A了。。否则TLE

*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAX = 500 + 10;
int L[MAX], N[MAX], M[MAX];
int LN[1000100],Ln[1000100];
int l, n, m, q;




int main()
{
    int cas = 0;
    while(~scanf("%d%d%d", &l, &n, &m)){
        for(int i = 1; i <= l; i++)
            scanf("%d", &L[i]);
        for(int i = 1; i <= n; i++)
            scanf("%d", &N[i]);
        for(int i = 1; i <= m; i++)
            scanf("%d", &M[i]);
        int cout = 1;
        for(int i = 1; i <= l; i++){
            for(int j = 1; j <= n; j++){
                LN[cout++] = L[i] + N[j];
            }
        }
        sort(LN + 1, LN + cout);
        int cout1 = 1;
        for(int i = 1; i < cout; i++){
            if(LN[i] != LN[i+1]) ;
            Ln[cout1++] = LN[i];
        }
        scanf("%d", &q);
        cas++;
        printf("Case %d:\n", cas);
        int p;
        for(int i = 1; i <= q; i++){
            scanf("%d", &p); 
            int flag = 0;
            for(int  j = 1; j <= m; j++){
                int ll = 1, rr = cout1 - 1;
                while(ll <= rr){
                    int mid = (ll + rr) >> 1;
                    if(Ln[mid] + M[j] == p){
                    //    printf("%d %d\n", mid, M[j]);
                        flag = 1;
                        break;
                    }
                    else if(Ln[mid] + M[j] > p)
                        rr = mid - 1;
                    else ll = mid + 1;
                }
                if(flag == 1) break;
            }
            if(flag == 1) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

  

posted @ 2015-08-01 15:15  Painting、时光  阅读(129)  评论(0编辑  收藏  举报