• hdu 2143 数组合并 二分


    Can you find it?

    Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
    Total Submission(s): 22312    Accepted Submission(s): 5627


    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
    题目大意:给你三个数列,然后给你若干个sum,问你能否从每个数列中各取出一个数,使得它们的和为sum,如果可以,输出YES,
    反之输出NO。
    思路分析:很明显暴力做会超时,应该采用先合并数组然后二分查找,时间复杂度降到了nlogn,但是在实现的时候还是有几点要注意,
    首先是输出格式,先 输出Case,然后再输入s,另外由于粗心大意,low和high的声明写到了for循环外面,wa到死,沧桑。
    代码:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <stack>
    #define mem(a) memset(a,0,sizeof(a))
    using namespace std;
    const int inf=0xffffff;
    const int maxn=500+50;
    __int64 a[maxn],b[maxn],c[maxn];
    __int64 f[maxn*maxn];
    int kase=0;
    int main()
    {
        int L,N,M;
        __int64 s,sum;
        while(scanf("%d%d%d",&L,&N,&M)!=EOF)
        {
            int num=0;
            for(int i=0;i<L;i++)
                scanf("%I64d",&a[i]);
            for(int i=0;i<N;i++)
                scanf("%I64d",&b[i]);
            for(int i=0;i<M;i++)
                scanf("%I64d",&c[i]);
            for(int i=0;i<L;i++)
            {
                for(int j=0;j<N;j++)
                {
                    f[num++]=a[i]+b[j];
                }
            }
            sort(f,f+num);
           printf("Case %d: ",++kase);
            scanf("%I64d",&s);
            while(s--)
            {
                scanf("%I64d",&sum);
                int flag=0;
            for(int i=0;i<M;i++)
            {
                 int low=0,high=L*N-1;
                if(flag) break;
                while(low<=high)
                {
                int mid=(high+low)/2;
                if(f[mid]==sum-c[i])
                {
                    flag=1;
                    break;
                }
                else if(f[mid]<sum-c[i]) low=mid+1;
                 else  high=mid-1;
                }
            }
             if(flag) cout<<"YES"<<endl;
             else cout<<"NO"<<endl;
            }
        }
        return 0;
    }
    tip:直接调用二分查找函数binary_search也可以解决
  • 相关阅读:
    arcgis10.2转shp文件中文乱码问题解决方案
    Android Context作为参数传递this
    andriod inputbox
    andriod inputType
    《ArcGIS Runtime SDK for Android开发笔记》——(5)、基于Android Studio构建ArcGIS Android开发环境(离线部署)(转)
    终于理解了什么是LGPL
    产品经理如何与强势的技术沟通? 技术比较有资历,会以技术无法实现等方面的原因拒绝处理产品提出的需求。 你们是否遇到这样的技术? 产品懂技术的话,是不是会好一些,因为可以和技术说“行话”了,并且产品懂技术就不会被忽悠了。
    Core Dump总结
    LIBRARY_PATH是编译时候用的,LD_LIBRARY_PATH是程序运行是使用的
    如何禁止C++默认成员函数
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5486402.html
Copyright © 2020-2023  润新知