HDU 2141 Can you find it?(二分)

Posted wzzkaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2141 Can you find it?(二分)相关的知识,希望对你有一定的参考价值。

题目链接:clicl here~~

【题目大意】:

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.

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
【解题思路】将前两个数组元素合并为一个,在和最后一个进行选择。二分合并后的数组,假设

   if(sort[mid]<ans) left=mid+1;
   else if(sort[mid]>ans) right=mid-1;
   else {flag=true;break;}

代码:

#include <bits/stdc++.h>
using namespace std;
const int N=505;
int A[N],B[N],C[N],D[N*N];
bool get(int sort[],int k,int ans)//合并后的数组/数组元素个数/和减去第三个数组元素剩下的值
{
    bool flag=false;
    int left=0,right=k,mid;
    while(left<=right)
    {
        mid=(left+right)>>1;
        if(sort[mid]<ans) left=mid+1;
        else if(sort[mid]>ans) right=mid-1;
        else {flag=true;break;}
    }
    return flag;
}
int main()
{
    //freopen("1.txt","r",stdin);
    int l,n,z,m,S,tot=1;
    bool ok;
    while(scanf("%d%d%d",&l,&n,&m)!=EOF)
    {
        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]);
        int k=0;
        for(int i=0;i<l;i++)
        for(int j=0;j<n;j++) D[k++]=A[i]+B[j];
        sort(D,D+k);
        scanf("%d",&S);
        printf("Case %d:\n",tot++);
        while(S--)
        {
            ok=false;
            scanf("%d",&z);
            for(int i=0;i<m;i++){
            if(get(D,k,z-C[i])) {ok=true;break;}
            }
            if(ok) puts("YES");
            else puts("NO");
        }
    }
    return 0;
}


以上是关于HDU 2141 Can you find it?(二分)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2141 Can you find it? 二分查找

HDU 2141 Can you find it? (二分)

hdu2141Can you find it?

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

HDU 2141 can you find it 二分

hdoj-2141-Can you find it?