zoukankan      html  css  js  c++  java
  • Can you find it?

    Can you find it?

    Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
    Total Submission(s): 1140 Accepted Submission(s): 370
     
    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
     
    Recommend
    威士忌
     
    /*
    二分查找嘛
    第一遍想的在三个数组整合到一起查找,但是超内存,想了一下,只整合两个数组的话,时间上虽然复杂了,但是内存小了
    */
    #include<bits/stdc++.h>
    #define MAX 505
    using namespace std;
    long long a[MAX],b[MAX],c[MAX],d[MAX*MAX];
    int BinarySearch(long long num[],long long end,long long n)/*二分查找*/
    {
        long long l=0,r=end,mid;
        while(l<=r)
        {
            mid=(l+r)/2;
            if(num[mid]==n)
                return 1;
            if(num[mid]>n)
                r=mid-1;
            else if(num[mid]<n)
                l=mid+1;
        }
        if(num[l]==n)
            return 1;
        return 0;
    }
    int main()
    {
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        long long L,N,M,t,n;
        int Case=1;
        while(scanf("%lld%lld%lld",&L,&N,&M)!=EOF)
        {
            for(int i=0;i<L;i++)
                scanf("%lld",&a[i]);
            for(int i=0;i<N;i++)
                scanf("%lld",&b[i]);
            for(int i=0;i<M;i++)
                scanf("%lld",&c[i]);
            long long len=0;
            for(int i=0;i<L;i++)
                for(int j=0;j<N;j++)
                        d[len++]=a[i]+b[j];
            sort(d,d+len);
            scanf("%lld",&t);
            printf("Case %d:
    ",Case++);
            while(t--)
            {
                scanf("%lld",&n);
                int f=0;
                for(int i=0;i<M;i++)
                {
                    if(BinarySearch(d,len,n-c[i]))
                    {
                        f=1;
                        break;
                    }    
                }
                if(f)
                    puts("YES");
                else
                    puts("NO");
            }
        }
    }
  • 相关阅读:
    计算机科学引论笔记
    Bootstrap实战 瀑布流布局
    前端性能和加载体验优化实践(附:PWA、离线包、内存优化、预渲染)
    Promise + Async&Await + Array.reduce + 函数递归 解决网络/接口请求的依次/排队不间断间隔访问
    webfunny前端监控新产品发布:点位系统上线,简单6步操作
    Java 全栈知识体系(2021 PDF 版本)
    Java 面试题及答案整理(2021最新版)持续更新中~~~
    python相关常见安装问题
    获取某一个数的2进制位数以及bitmask
    centos7下利用qemu搭建arm模拟器
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5994656.html
Copyright © 2011-2022 走看看