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

原创 2018年04月17日 11:05:26

题意

给出A, B, C三个数组, 长度分别为l, m, n ( 1 <= l, m, n <= 500 )
给出s (1 <= s <= 1000)个查询, 查找是否存在x使得在A, B, C中各取一个数使得a+b+c == x

思路

500*500*500*1000 复杂度太高无法暴力求解
考虑到用二分查找
先将前两个数列相加产生ab[]数列,这时ab+c = x,那么ab= x-c,每次查询x时,遍历c中的所有数,求得x-c,在ab数列中二分查找。
手写二分查找或者用STL中的binary_search都可以

二分查找变形合集 :
你真的会写二分查找吗?

STL中的二分查找:
STL之二分查找(binary_search(),lower_bound(),upper_bound() )
upper_bound 和lower_bound 的返回值问题

AC代码(手写二分查找)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define mst(a) memset(a, 0, sizeof(a))
using namespace std;
const int maxn = 500+5;
typedef unsigned long long ll;
ll a[maxn], b[maxn], c[maxn], ab[maxn*maxn];

void init(){
    mst(a);mst(b);mst(c);mst(ab);
}

int binsearch(ll arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;
    while(left <= right){
        mid = (left + right)/2;
        if (key <= arr[mid])  right = mid - 1;
        else   left = mid + 1;
    }
    if(left < len && arr[left] == key)
        return left;
    return -1;
}

int main()
{
    int l, m, n, k, t;
    int kase = 0;
    while( ~scanf("%d%d%d",&l, &m, &n) ){
        init();
        for( int i = 0; i < l; i++ )   scanf("%llu",&a[i]);
        for( int i = 0; i < m; i++ )   scanf("%llu",&b[i]);
        for( int i = 0; i < n; i++ )   scanf("%llu",&c[i]);
        printf("Case %d:\n",++kase);
        sort(c, c+n);
        k = -1;
        for( int i = 0; i < l; i++ )
            for( int j = 0; j < m; j++ )
                ab[++k] = a[i] + b[j];
        sort(ab, ab+k);
        int len = k;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&k);
            bool flag = false;
            for( int i = 0; i < n; i++ ){
                int xx = k - c[i];
                if( binsearch( ab, len, xx ) != -1 ){
                    printf("YES\n");
                    flag = true;
                    break;
                }
            }
            if(!flag) printf("NO\n");
        }
    }
    return 0;
}

AC代码(STL)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

#define mst(a) memset(a, 0, sizeof(a))
using namespace std;
const int maxn = 500+5;
typedef long long ll;
ll a[maxn], b[maxn], c[maxn], ab[maxn*maxn];

void init(){
    mst(a);mst(b);mst(c);mst(ab);
}

int main()
{
    int l, m, n, k, t;
    int kase = 0;
    while( ~scanf("%d%d%d",&l, &m, &n) ){
        init();
        for( int i = 1; i <= l; i++ )   scanf("%lld",&a[i]);
        for( int i = 1; i <= m; i++ )   scanf("%lld",&b[i]);
        for( int i = 1; i <= n; i++ )   scanf("%lld",&c[i]);
        printf("Case %d:\n",++kase);
        sort(c+1, c+n+1);
        k = 0;
        for( int i = 1; i <= l; i++ )
            for( int j = 1; j <= m; j++ )
                ab[++k] = a[i] + b[j];
        sort(ab+1, ab+k+1);
        int len = k;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&k);
            bool flag = false;
            for( int i = 1; i <= n; i++ ){
                int xx = k - c[i];
                if( binary_search( ab+1, ab+len+1, xx ) != 0 ){
                    printf("YES\n");
                    flag = true;
                    break;
                }
            }
            if(!flag) printf("NO\n");
        }
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JinxiSui/article/details/79971875

数据结构基础系列(8):查找

-
  • 1970年01月01日 08:00

HDU2141:Can you find it?(二分)

Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now y...
  • libin56842
  • libin56842
  • 2013-11-24 14:41:35
  • 3363

HDOJ2141Can you find it?【二分查找】

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Other...
  • R1986799047
  • R1986799047
  • 2015-07-30 15:38:12
  • 503

[ACM] hdu 2141 Can you find it? (二分查找)

Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo...
  • sr19930829
  • sr19930829
  • 2014-02-24 16:07:03
  • 1658

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

上题目Time Limit:3000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description Give y...
  • sun897949163
  • sun897949163
  • 2015-12-06 19:03:48
  • 306

二分查找:hdu 2141 Can you find it?

Can you find it? Problem Description Give you three sequences of numbers A, B, C, then we give you ...
  • u012717411
  • u012717411
  • 2014-12-23 20:43:28
  • 635

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

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Other...
  • m0_37624640
  • m0_37624640
  • 2017-08-19 09:31:28
  • 74

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

Can you find it? Time Limit: 3000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u ...
  • qq_18738333
  • qq_18738333
  • 2015-09-16 20:04:33
  • 330

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值。 这里简单介绍一个小算法,二分查找。 1 /...
  • qiqi_skystar
  • qiqi_skystar
  • 2015-10-12 14:19:13
  • 247

HDU 2141 can you find it?【二分查找】

can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others...
  • mengxiang000000
  • mengxiang000000
  • 2016-03-28 20:44:31
  • 317
收藏助手
不良信息举报
您举报文章:HDU 2141 - Can you find it? ( 二分查找 )
举报原因:
原因补充:

(最多只允许输入30个字)