• 二分查找,三分查找


    题意:   先输入一个整数,后在输入n个整数(保证)输入的序列为有序序列(递增或递减),最后输入一个整数m,问该序列是否存在m,若存在则输出YES,否则输出NO

    简言之就是在一个有序数组里面查找是否存在m

     二分如下  :

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define Mem0(x) memset(x,0,sizeof(x))
    #define Mem1(x) memset(x,-1,sizeof(x))
    #define MemX(x) memset(x,0x3f,sizeof(x));
    using namespace std;
    typedef long long ll;
    const int inf=0x3f3f3f;
    const double eps=1e-12;
    int m,a[1010],flag=0;
    void binsearch1(int a[],int start,int end) //递归
    {
    int mid=(start+end)/2;
    if (a[mid]==m){
    flag=1;
    printf("YES ");
    }
    else if (m>a[mid])
    binsearch1(a,mid+1,end);
    else if (m<a[mid])
    binsearch1(a,start,mid-1);
    }
    void binsearch2(int a[],int start,int end)
    {
    while (start<=end&&!flag){
    int mid=(start+end)/2;
    if (a[mid]>m)
    end=mid-1;
    else if(a[mid]<m)
    start=mid+1;
    else{
    printf("YES ");
    flag=1;
    }
    }
    }
    int main()
    {
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    cin>>a[i];
    cin>>m;
    binsearch1(a,0,n-1); //递归版
    binsearch2(a,0,n-1); //while循环版
    if (flag==0)
    cout<<"NO";
    return 0;
    }

    二分查找有有点也有缺陷,优点:大大提高了查找的效率,  缺陷:只能查钊单调递增的序列

    假若要查找一个抛物线的最值的时候,这里是二分法是不适用的,这里适用三分。

     HDU 2899

    Problem Description
    Now, here is a fuction:
      F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
    Can you find the minimum value when x is between 0 and 100.
     
    
    Input
    The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
     
    
    Output
    Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
     
    
    Sample Input
    2
    100
    200
     
    
    Sample Output
    -74.4291
    -178.8534
     
    
    Author
    Redow
    

      

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<set>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define Swap(a,b,t) t=a,a=b,b=t
    #define Mem0(x) memset(x,0,sizeof(x))
    #define Mem1(x) memset(x,-1,sizeof(x))
    #define MemX(x) memset(x,0x3f,sizeof(x))
    using namespace std;
    typedef long long ll;
    const int inf=0x3f3f3f;
    const double eps=1e-6;
    //  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
    int y;
    double solve(double x)
    {
        return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
    }
    int main()
    {
        int t;
        cin>>t;
        while (t--){
            scanf("%d.%s",&num,&str);
            cin>>y;
            double l=0.0,r=100.0;
            while (r-l>=eps){
                double mid=(l+r)/2;
                double mmid=(mid+r)/2;        
                if(solve(mid)<solve(mmid)){
                    r=mmid;
                }
                else
                    l=mid;
            }
            printf("%.4lf
    ",solve(l));
        }
        return 0;
    }
  • 相关阅读:
    在linux上搭建sftp服务
    FTP客户端遇到150连接超时错误的处理办法
    电脑每次开机打开微软网站怎么解决
    Linux学习笔记之认识与学习Bash
    Linux学习笔记之VIM编辑器
    Linux学习笔记之文件与文件系统的压缩与打包
    Linux学习笔记之磁盘与文件系统的管理
    Linux学习笔记之目录配置
    Linux学习笔记之档案权限与目录配置
    ubuntu修改apt源
  • 原文地址:https://www.cnblogs.com/q1204675546/p/9341676.html
Copyright © 2020-2023  润新知