zoukankan      html  css  js  c++  java
  • HDU 2141 二分查找

    还有http://blog.csdn.net/libin56842/article/details/17336981没做
    数据结构http://blog.csdn.net/olga_jing/article/details/50912239
    字典树

    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


     

    题目意思是:给你3个数组a[],b[],c[],再给你s个询问,对于每个询问X,若能在a,b,c三个数组各找1个数字,使得他们的和为X,则输出YES否则输出NO。我的思路是在a,b数组中按顺序各取一个数加起来放入新数组num中,对num数组和c数组进行排序。排序后,遍历排序后的c数组,查询num数组中是否有X-ci,若有则输出YES,否则输出NO



    最重要的是用对数据类型,保存a与b之和要用long long或__int64





    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<math.h>
    #define N 505
    using namespace std;
    __int64 num[N*N];
    int mybinary_search(int len,int target)
    {
        int L,R,mid=0;
        L=0;
        R=len-1;
        while(L<=R)
        {
            mid=(L+R)>>1;
            if(num[mid]>target)R=mid-1;
            else if(num[mid]<target)L=mid+1;
            else return mid;
        }
        return -1;
    }
    
    int main()
    {
        int i,j,k,l,n,m,query,cnt1,cnt2;
        int a,b,c,flag;
        int  aa[505],bb[505],cc[505];
        cnt2=0;
        while(scanf("%d%d%d",&l,&m,&n)!=EOF)
        {
            cnt1=0;
            for(i=0; i<l; i++)scanf("%d",&aa[i]);
            for(i=0; i<m; i++)scanf("%d",&bb[i]);
            for(i=0; i<n; i++)scanf("%d",&cc[i]);
            for(i=0; i<l; i++)
                for(j=0; j<m; j++)
                    num[cnt1++]=aa[i]+bb[j];
            sort(num,num+cnt1);
            sort(cc,cc+n);
            scanf("%d",&k);
            printf("Case %d:
    ",++cnt2);
            while(k--)
            {
                scanf("%d",&query);
                flag=0;
                if(query<num[0]+cc[0]||query>num[cnt1-1]+cc[n-1])
                {
                    printf("NO
    ");
                    continue;
                }
                for(j=0; j<n; j++)
                {
                    if(mybinary_search(cnt1,query-cc[j])!=-1)
                    {
                        printf("YES
    ");
                        flag=1;
                        break;
                    }
                }
                if(!flag)printf("NO
    ");
            }
        }
    
        return 0;
    }





  • 相关阅读:
    STL Allocator
    Several NeedToKnow(assert/stdin/stdout/CString/Standard C++ Library)
    VS Project Property Sheet
    进度总结(3)
    进度总结(2)
    进度总结(4)
    进度总结(7)
    进度总结(1)
    进度总结(5)
    进度总结(6)
  • 原文地址:https://www.cnblogs.com/hjch0708/p/7554837.html
Copyright © 2011-2022 走看看