zoukankan      html  css  js  c++  java
  • Hdu Can you find it?(二分答案)

    Can you find it?
    Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
    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
    Source
    HDU 2007-11 Programming Contest

    /*
    二分答案.
    比较巧妙.
    先将两个数组合并搞成一个n^2大的数组.
    然后二分的话复杂度就有一个log.
    二分和合并后的数组即对n^2log.
    然后复杂度就大大降低了.
    */
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #define MAXN 501
    #define LL long long
    using namespace std;
    LL s[MAXN*MAXN],a[MAXN],b[MAXN],c[MAXN],n1,n2,n3,n,m;
    LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
        return x*f;
    }
    bool erfen(int l,int r,int i,int x)
    {
        int mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(s[mid]+c[i]==x) return true;
            if(s[mid]+c[i]>x) r=mid-1;
            else l=mid+1;
        }
        return false;
    }
    void slove()
    {
        int x;
        bool flag;
        while(m--)
        {
            flag=false;x=read();
            for(int i=1;i<=n3;i++)
              if(erfen(1,n,i,x)){flag=true;printf("YES
    ");break;}
            if(!flag) printf("NO
    ");
        }
        return ;
    }
    int main()
    {
        int t=0;
        while(~scanf("%d%d%d",&n1,&n2,&n3))
        {
            printf("Case %d:
    ",++t);n=0;
            for(int i=1;i<=n1;i++) a[i]=read();
            for(int i=1;i<=n2;i++) b[i]=read();
            for(int i=1;i<=n3;i++) c[i]=read();
            for(int i=1;i<=n1;i++)
              for(int j=1;j<=n2;j++)
                s[++n]=a[i]+b[j];
            sort(s+1,s+n+1);
            m=read();slove();
        }
        return 0;
    }
  • 相关阅读:
    进程锁和进程池(附线程池)
    Python多进程
    queue队列
    随笔:python3+yagmail邮件发送,简单易操作
    随笔:用python的pip命令时却显示没有提供命令:Did not provide a command
    随笔:python+selenium+unittest用qq邮箱上传文件并发送邮件
    随笔:用HtmlTestRunner生成报告
    随笔:Linux里mysql的tar包安装
    Performance Testing 前期准备以及场景设计
    Performance Testing 入门小结
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068111.html
Copyright © 2011-2022 走看看