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

来源:互联网 发布:python unicode转json 编辑:程序博客网 时间:2024/05/07 14:19

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 31 2 31 2 31 2 331410
 

Sample Output
Case 1:NOYESNO

【思路分析】
   给你一个整数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;}


0 0
原创粉丝点击