zoukankan      html  css  js  c++  java
  • 二分(1)

    题目:

    3

    10

    1 3 5

    3个数组1  3 5中放回的选取4个数字和为10


    思路:暴力可以写,但是复杂度为o(n^4)

             二分快排   复杂度为o(n^2*logn)

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<algorithm>
    #include<map>
    #define maxn 100005
    using namespace std;
    int mp[maxn];
    int n,m;
    int k[maxn];
    bool panduan(int x)
    {
        int l=0,r=n*n;
        while(r-l>=1)
        {
            int i=(r+l)/2;
            if(mp[i]==x)return true;
            else if(mp[i]<x)l=i+1;
            else {
                r=i;
            }
        }
        return false;
    }
    int main()
    {
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            cin>>k[i];
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
        {
            mp[i*n+j]=k[i]+k[j];
        }
        sort(mp,mp+n*n);
        bool f=false;
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            if(panduan(m-k[i]-k[j]))f=true;
        }
        if(f)cout<<"YSE"<<endl;
        else cout<<"NO"<<endl;
        return 0;
    }
  • 相关阅读:
    跨域与跨域访问
    bootstrap.min.css.map作用
    CSS
    http协议
    djngo未整理
    redis安装及配置
    git 基础
    yum安装报错
    Go语言与区块链开发(转载)
    electron实现MessageBox
  • 原文地址:https://www.cnblogs.com/huangzzz/p/7885992.html
Copyright © 2011-2022 走看看