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

题意

给出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
文章标签: 二分查找 STL
个人分类: 二分 STL
上一篇HDU 6188 - Duizi and Shunzi ( 贪心 )
下一篇HDU 6182 - A Math Problem
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭