• Can you find it?——[二分查找]


    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
     

    解题思路:

      首先我们考虑这个问题:给定两个序列A,B,和确定的数x,问是否存在i,j使满足A[i]+B[j]=x的?最快的方法是枚举A,然后在B中二分查找

    x-A。现在回到这个问题,这道题给了三组序列A,B,C如何查找呢?我们不妨将A,C两数组合并成新数组LN,LN中每个元素都是Ai+Bj的和。然后枚举B,在LN中二分查找x-B。

    *这里有个细节:由于x为32位整数,当A+B>INT32_MAX时,可以不用加入LN.

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <time.h>
     5 using namespace std;
     6 #define clock__ printf("%f
    ",double(clock())/CLOCKS_PER_SEC);
     7 #define maxn 500
     8 #define INT_32_MAX ((1<<31)-1)
     9 typedef long long LL;
    10 int A[maxn+3],B[maxn+3],C[maxn+3];
    11 int L,M,N,S;
    12 int LN[maxn*maxn+5];
    13 int h;
    14 
    15 bool search_LN(int a,int b,int x){
    16     int len=b-a;
    17     int mid=a+len/2;
    18     if(len==1){
    19         if(LN[a]==x) return true;
    20         else return false;
    21     }
    22     if(LN[mid]==x) return true;
    23     else if(LN[mid]>x) return search_LN(a, mid, x);
    24     else return search_LN(mid, b, x);
    25 }
    26 
    27 int main() {
    28 
    29     int T=0;
    30     while(scanf("%d%d%d",&L,&M,&N)==3){
    31         printf("Case %d:
    ",++T);
    32         for(int i=0;i<L;i++)
    33             scanf("%d",&A[i]);
    34         for(int i=0;i<M;i++)
    35             scanf("%d",&B[i]);
    36         for(int i=0;i<N;i++)
    37             scanf("%d",&C[i]);
    38         h=0;
    39         for(int i=0;i<L;i++)
    40             for(int j=0;j<N;j++){
    41                 if(LL(A[i])+C[j]<=INT_32_MAX)
    42                 LN[h++]=A[i]+C[j];
    43             }
    44         sort(LN, LN+h);
    45         scanf("%d",&S);
    46         for(int i=1;i<=S;i++){
    47             int x;
    48             scanf("%d",&x);
    49             bool ok=0;
    50             for(int j=0;!ok&&j<M;j++){
    51                 if(search_LN(0,h, x-B[j]))
    52                     ok=1;
    53             }
    54             if(ok)printf("YES
    ");
    55             else printf("NO
    ");
    56         }
    57     }
    58     //clock__
    59     return 0;
    60 }
  • 相关阅读:
    sqlserver 分页
    sqlserver 用FOR XML PATH('')多行并成一列
    yarn的安装和使用
    redis安装及基本使用
    dbeaver 的界面乱码
    cypress测试框架(一)
    外网访问VMware虚拟机的Web服务---系列操作
    将博客搬至CSDN
    textgrid-python模块基础使用
    opencv通过mask掩码图合成两张图
  • 原文地址:https://www.cnblogs.com/Kiraa/p/5306109.html
Copyright © 2020-2023  润新知