码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-01 15:42:41      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

  

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

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4694140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!