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): 38634    Accepted Submission(s): 9395


    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
     

    题目大意:

    多组数据输入

    第一行是三个序列各自元素的个数

    第2,3,4是三个序列

    第4行是x的个数

    下面的x行是多种情况的x值

    分析:

    三个序列三个数组,把前面两个数组组合为一个数组

    比如1 2 3,1 2 3,这两个序列组合为一个数组‘

    1+1,1+2,1+3,2+1,2+2,2+3,3+1,3+2,3+3,

    计算一下就是

    2,3,4,3,4,5,4,5,6,有重复的但是不用去掉,称这个序列为t

    然后与第三个序列组合

    1 ,2,3,(第三个序列)

    然后将t序列升序排序,t与第三个序列组合的时候,t序列采用二分寻找数据

    注意点:

    1.不能采用set去重,不然会超内存(第一次遇到超内存,好激动啊啊啊啊啊)

    2.对t序列不能采用for循环寻找数据,要采用排好序之后再二分查找数据,不然会超时

    代码如下:

    #include<bits/stdc++.h>
    using namespace std;
    bool cmp(int a,int b)
    {
        return a<b;
    }
    int main()
    {
        int n1,n2,n3,n=1;
        while(~scanf("%d %d %d",&n1,&n2,&n3))
        {
            int a[n1],b[n2],c[n3];
            for(int i=0; i<n1; i++)
            {
                scanf("%d",&a[i]);
            }
            for(int j=0; j<n2; j++)
            {
                scanf("%d",&b[j]);
            }
            for(int k=0; k<n3; k++)
            {
                scanf("%d",&c[k]);
            }
            int xn;
            int t[n1*n2];
            scanf("%d",&xn);
            int x[xn];
            for(int i=0; i<xn; i++)
            {
                scanf("%d",&x[i]);
            }
            int y=0;
            for(int i=0; i<n1; i++)
            {
                for(int j=0; j<n2; j++)
                {
                    int z=a[i]+b[j];
                    t[y]=z;
                    y++;
                }
            }
            sort(t,t+(n1*n2),cmp);
            printf("Case %d:
    ",n);
            for(int i=0; i<xn; i++)
            {
                int f=0;
                for(int k=0;k<n3;k++)
                {
                    int l=0,h=n1*n2-1;
                    while(l<=h)
                    {
                        int mid=(l+h)/2;
    
                        if(t[mid]==x[i]-c[k])
                        {
                            f=1;
                            break;
                        }else if(t[mid]>x[i]-c[k])
                        {
                            h=mid-1;
                        }else if(t[mid]<x[i]-c[k])
                        {
                            l=mid+1;
                        }
                    }
                    if(f==1)
                        break;
                }
                if(f==1)
                {
                    printf("YES
    ");
                }
                else
                {
                    printf("NO
    ");
                }
            }
            n++;
        }
        return 0;
    }

    哈哈哈哈哈哈哈哈哈哈

    今天真的是超级开心

    不准吐槽。。。。。

    。。。。。。。。。。 

  • 相关阅读:
    JDK8:Lambda表达式
    静态代理模式
    javaEE servlet tomcat jsp对应关系
    cmake 实现交叉编译注意事项
    vector 初始化
    今儿谈谈:互联网创业公司,CTO,CFO,CEO们各负责什么?
    解答网友问:继续学习计算机还是转成会计专业!?如何做正确选择!
    谈谈关于男孩子学会计什么样?有没有发展钱途!
    代码时代!码农所创造财务机器人是财务人的解药还是毒药!?
    谈谈关于人工智能对财务会计行业的影响
  • 原文地址:https://www.cnblogs.com/yinbiao/p/8903749.html
Copyright © 2011-2022 走看看