zoukankan      html  css  js  c++  java
  • hdu2141

    Can you find it?

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


    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
     
    思路:二分法,将Ai+Bj的值存入数组sum中,然后用二分法检测(X-sum[i])是否等于Ck
     
    代码:
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int a[510],b[510],c[510],sum[260000],k;
    int main()
    {
        int i,j,x,cnt=1,s,l,m,n,flag,tag,left,right,mid;
        while(scanf("%d%d%d",&l,&n,&m)!=EOF)
        {
            for(i=0; i<l; i++)
                scanf("%d",&a[i]);
            for(i=0; i<n; i++)
                scanf("%d",&b[i]);
            for(i=0; i<m; i++)
                scanf("%d",&c[i]);
            k=0;
            for(i=0; i<l; i++)
                for(j=0; j<n; j++)
                    sum[k++]=a[i]+b[j];
            sort(sum,sum+k);
            scanf("%d",&s);
            printf("Case %d: ",cnt++);
            while(s--)
            {
                scanf("%d",&x);
                flag=0;
                for(i=0; i<m; i++)
                {
                    int t=x-c[i];
                    left=0;
                    right=k-1;
                    tag=0;
                    while(left<=right)
                    {
                        mid=(left+right)>>1;
                        if(sum[mid]>t)
                            right=mid-1;
                        else if(sum[mid]<t)
                            left=mid+1;
                        else
                        {
                            tag=1;
                            break;
                        }
                    }
                    if(tag)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                    printf("YES ");
                else
                    printf("NO ");
            }
        }
        return 0;
    }
  • 相关阅读:
    java第九周上机
    安卓-计算器
    安卓第四周作业
    第十五周作业
    第十三周作业
    第十三周上机作业
    第十二周作业
    第十二周上机作业
    第十一周作业
    第十一周上机作业
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3442123.html
Copyright © 2011-2022 走看看