zoukankan      html  css  js  c++  java
  • HDU 2141 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): 21485    Accepted Submission(s): 5446

    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
     
    Recommend
    威士忌
     
    题目大意:输入三个数l, m, n。代表序列A(a1,a2,a3....ai),B(b1,b2,b3...bi),C(c1,c2,c3...ci),分别有l,m,n个数,接下来输入3个序列,再输入S,代表S组数据,接下来输入S组数据,判断这些数能否等于AI+BJ+CK(i,j,k任意)
     
    思路:开始用暴力3个for,会超时。想到用二分,可以用一个新数组sum存入a+b所有可能,用sort排序,再判断x-c能否在sum数组找到。
     
    #include<iostream>
    #include<algorithm>
    int a[510], b[510], c[510],sum[250010];
    using namespace std;
    int f(int x,int z,int y)//二分注意加一 
    {
        int mid; 
        while(z<=y)
        {
            mid=(z+y)/2;
            if(sum[mid]==x) return 1;
            else if(sum[mid]>x)
            y=mid-1;
            else if(sum[mid]<x)
            z=mid+1;
        }
        return 0;
    }
    int main()
    {
        int l,m,n,z,i,j,t,k=1;
        while(scanf("%d%d%d", &l, &m, &n)!=EOF)
        {
            for(i=0;i<l;i++)scanf("%d",&a[i]);//一开始l,m,n写乱了,WA了好几遍,玛德智障。。。
            for(i=0;i<m;i++)scanf("%d",&b[i]);
            for(i=0;i<n;i++)scanf("%d",&c[i]);
            for(i=0;i<l;i++)
                for(j=0;j<m;j++)
                    sum[i*m+j]=a[i]+b[j];
            sort(sum,sum+l*m);
            printf("Case %d:
    ",k++);
            scanf("%d",&t);
            while(t--)
            {
                scanf("%d",&z);
                for(j=0;j<n;j++)
                    if(f(z-c[j],0,l*m))
                    break;
                if(j!=n)
                printf("YES
    ");
                else
                printf("NO
    ");
            }
        }
        return 0;
    }
     
  • 相关阅读:
    UIImagePickerController从拍照、图库、相册获取图片
    swift2.0 UIImagePickerController 拍照 相册 录像
    UIImagePickerController拍照与摄像
    iOS 从相机或相册获取图片并裁剪
    Android播放音频的两种方式
    UICollectionView的基本使用
    UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图
    UIView属性clipsTobounds的应用
    iOS开发UI篇—CALayer简介
    chrome插件演示,经js转让chrome api清除浏览器缓存
  • 原文地址:https://www.cnblogs.com/Noevon/p/5327217.html
Copyright © 2011-2022 走看看