• Can you find it?(数组+二分hdu2141)


    Can you find it?

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

    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
     
    题意:给你三组数据,每组有L,N,M个数,每组取一个相加,问在他们的和数组里有没有x这个数?;
     
    思路:数组+二分。。。。。
     
     
    详见代码:
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int cmp(int x,int y)
    {
        return x<y;
    }
    __int64 num[300000];
    int main()
    {
        int S,ans,i,j,k,temp,t=1;
        int L,N,M,a[505],b[505],c[505],p;
    
        int left,right,middle;
        while(scanf("%d%d%d",&L,&N,&M)!=EOF)
        {
    //        memset(num,0,sizeof(num));
            for(i=0;i<L;i++)
            {
                scanf("%d",&a[i]);
            }
    //        sort(a,a+L,cmp);
            for(i=0;i<N;i++)
            {
                scanf("%d",&b[i]);
            }
    //        sort(b,b+N,cmp);
            for(i=0;i<M;i++)
            {
                scanf("%d",&c[i]);
            }
    //        sort(c,c+M,cmp);
            ans=0;
            for(i=0;i<L;i++)
                for(j=0;j<N;j++)
                {
                    num[ans++]=a[i]+b[j];
                }
            sort(num,num+ans,cmp);
            printf("Case %d:
    ",t++);
            scanf("%d",&S);
            while(S--)//for(i=0;i<S;i++)写成这样,,,那怪超时。。。顿时郁闷了!
            {
                temp=0;
                scanf("%d",&p);
                for(i=0;i<M;i++)
                {
                    if(num[0]<=p-c[i]&&p-c[i]<=num[ans-1])
                    {
                        left=0;
                        right=ans-1;
    
                        while(right-left>=0)
                        {
                            middle=(left+right)/2;
    
                            if(num[middle]>p-c[i])
                            {
                                right=middle-1;
                            }
                            else if(num[middle]<p-c[i])
                            {
                                left=middle+1;
                            }
                            else
                            {temp=1;break;}
                        }
                        
                    }
                    else
                        temp=0;
                    if(temp)
                        break;
                }
                if(temp)
                    printf("YES
    ");
                else
                    printf("NO
    ");
            }
        }
        return 0;
    }
    /*
    3 3 3
    1 2 3
    1 2 3
    1 2 3
    11
    4
    1
    10
    9
    8
    7
    6
    5
    3
    2
    11
    */
  • 相关阅读:
    java 线程之间的协作 wait()与notifyAll()
    加密web.config中的邮件配置mailSettings
    TCP编程,Socket通讯
    jQuery插件学习笔记
    抹掉Scala的糖衣(14) -- Update Method
    UVA 12034 Race (递推神马的)
    struts2 命名空间 namespace 学习
    Vim -&gt; 移动光标
    【跟我一步一步学Struts2】——Struts2工作流程
    Python中sort以及sorted函数初探
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/3443905.html
Copyright © 2020-2023  润新知