zoukankan      html  css  js  c++  java
  • 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:
    ", 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
    ", 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
    ");
                else printf("NO
    ");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    在 Borland C++ 及 Visual C++ 环境中使用 STLport (作者:孟岩)
    设置JavaFX-CSS改变TreeView节点图标
    Using MS DataGrid control with ADO
    两个加载fxml文件的方法
    JavaFX中ObservableValue类型
    在 Eclipse 下利用 gradle 构建系统
    JavaFX初探
    深度剖析如何保证缓存与数据库的一致性
    ACID的实现原理
    一颗高度为3的B+树能存多少行数据?
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4694140.html
Copyright © 2011-2022 走看看