• 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:
    ",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;
    }
    


  • 相关阅读:
    windows上安装xampp和dvwa
    CentOS 7上安装Vtiger CRM Open Source Edition
    快速建站(lnmp)
    centos7中jdk安装
    centos7中apache安装
    centos7中mysql安装
    再探决策树算法之利用sklearn进行决策树实战
    决策树算法初探
    定制化自己的itchat
    itchat初探
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6849787.html
Copyright © 2020-2023  润新知