HDU 2141 Can you find it?(二分查找)

本博客介绍了一个算法问题,即在给定三个数组的情况下,寻找三个数(Ai,Bj,Ck)的组合,使其和等于给定的目标值X。通过使用动归、二分查找和去重函数等技巧,实现高效搜索。详细步骤包括数组排序、去重、计算和匹配等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Can you find it?



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

【思路分析】
   给你一个整数X,再给你三个数组,要你判断分别从这三个数组中取出一个数,这三个数的和是否为
X一开始我想到了动归里面的整数划分问题,即将一个整数划分为3个整数,然后再去判断这3个整数是否分别在三个数组里面,可是以前解决的问题是求整数划分的划分数,也就是一个整数划分为K个整数的划分方案数,而不能求出这些数,但我觉得这还是一个可以继续去探讨的问题,现在暂时留在这儿,等有思路了继续想。
   另一种正确的思路是二分查找,将A、B数组各个元素的和存入到另一个数组Sum,并对Sum进行排序、去重,令 ans = temp - C[i],然后再去Sum中查找是否存在ans,这里可以采用二分查找提高效率。
   另外补充一下去重函数的相关知识:unique()函数,其作用是去除右相邻元素中与之相同的元素,只保留一个,其他相同的元素则被移到当前数组最后一个元素之后,直到处理到最后一个保留的元素,并返回当前元素的地址,可见去重的过程并不是删除相同的元素。当然这个函数也完全可以手写出来。


代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 505;
int A[maxn],B[maxn],C[maxn];
__int64 sumAB[maxn * maxn];//A,B数组相加的结果可能会超过int的范围
int lenofSum;

bool bin_search(int x)
{
    int l = 0,r = lenofSum - 1;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(x > sumAB[mid])
        {
            l = mid + 1;
        }
        else if(x < sumAB[mid])
        {
            r = mid - 1;
        }
        else
            return true;
    }
    return false;
}

int main()
{
    int l,n,m;
    int cas = 1;
    while(scanf("%d %d %d",&l,&n,&m) != EOF)
    {
        printf("Case %d:\n",cas++);

        lenofSum = 0;
        for(int i = 0;i < l;i++)
            scanf("%d",&A[i]);
        for(int i = 0;i < n;i++)
            scanf("%d",&B[i]);
        for(int i = 0;i < m;i++)
            scanf("%d",&C[i]);
        sort(C,C + m);

        for(int i = 0;i < l;i++)
            for(int j = 0;j < n;j++)
            {
                sumAB[lenofSum++] = A[i] + B[j];//相加结果可能会超出int范围
            }
       sort(sumAB,sumAB + lenofSum);
       lenofSum = unique(sumAB,sumAB + lenofSum) - sumAB;
       //对sumAB数组内相邻的相同元素去重(因此之前需要排序)

       int s;
       scanf("%d",&s);
       while(s--)
       {
           int temp,ans,flag = 0;
           scanf("%d",&temp);
           if(temp < (sumAB[0] + C[0]) || temp > (sumAB[lenofSum - 1] + C[m - 1]))
           {
               printf("NO\n");
           }
           else
           {
               for(int i = 0;i < m;i++)
               {
                   ans = temp - C[i];
                   if(bin_search(ans))
                   {
                       flag = 1;
                       break;
                   }
               }
               if(flag == 1)
                printf("YES\n");
               else
                printf("NO\n");
           }

       }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值