二分法(蓝桥杯必考模板保姆级教程)

目录

精确查找

靠左精确查找

靠右精确查找

P2249 【深基13.例1】查找

题目描述

输入格式

输出格式

输入输出样例

说明/提示

区域查找

找左边界模板(以找最后个小于的数为例)

 找右边界模板(以找第一个大于的数为例)

例题:递增三元组

1.排序加二分法

2.前缀和

3.暴力

习题

P1024 [NOIP2001 提高组] 一元三次方程求解

题目描述

输入格式

输出格式

输入输出样例

说明/提示

P1163 银行贷款

题目描述

输入格式

输出格式

输入输出样例

说明/提示

秦九韶算法


必须多做题才能体会出二分法的细节之处

精确查找

靠左精确查找

a为查找的数组,l为左边界,r为右边界,v为所查询的目标

 private static int bsearch(int[] a, int l, int r, int v) {
  if(l>r)return -1;
  if(l==r){
      if(a[l]==v)return l;
      else return -1;
  }
  int mid=(l+r)/2;
  if(a[mid]>=v)return bsearch(a,l,mid,v);
  else return bsearch(a,mid+1,r,v);


    }

靠右精确查找

 private static int bsearch(int[] a, int l, int r, int v) {
        if(l>r)return -1;
        if(l==r){
            if(a[l]==v)return l;
            else return -1;
        }
        int mid=(l+r+1)/2;
        if(a[mid]<=v)return bsearch(a,mid,r,v);
        else return bsearch(a,l,mid-1,v);


    }

P2249 【深基13.例1】查找

题目描述

输入 nn 个不超过 10^9109 的单调不减的(就是后面的数字不小于前面的数字)非负整数 a_1,a_2,\dots,a_{n}a1​,a2​,…,an​,然后进行 mm 次询问。对于每次询问,给出一个整数 qq,要求输出这个数字在序列中第一次出现的编号,如果没有找到的话输出 -1−1 。

输入格式

第一行 22 个整数 nn 和 mm,表示数字个数和询问次数。

第二行 nn 个整数,表示这些待查询的数字。

第三行 mm 个整数,表示询问这些数字的编号,从 11 开始编号。

输出格式

输出一行,mm 个整数,以空格隔开,表示答案。

输入输出样例

输入 #1复制

11 3
1 3 3 3 5 7 9 11 13 15 15
1 3 6

输出 #1复制

1 2 -1 

说明/提示

数据保证,1 \leq n \leq 10^61≤n≤106,0 \leq a_i,q \leq 10^90≤ai​,q≤109,1 \leq m \leq 10^51≤m≤105

本题输入输出量较大,请使用较快的 IO 方式。

import java.*;
import java.io.*;
import java.util.Scanner;

public class Main {
static Scanner sc=new Scanner(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st=new StreamTokenizer(br);
static PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws Exception{
    st.nextToken();
    return (int)st.nval;
    }
    public static long nextLong() throws Exception{
        st.nextToken();
        return (long)st.nval;
    }
    public static double nextDouble() throws Exception{
        st.nextToken();
        return st.nval;
    }
    public static String next() throws Exception{
        st.nextToken();
        return st.sval;
    }
public static String nextLine() throws Exception{
    return br.readLine();
}
    public static void main(String[] args) throws Exception{
int n=nextInt();
int m=nextInt();
int a[]=new int[n+1];
for(int i=1;i<=n;i++){
    a[i]= nextInt();
}
while(m-->0){
    pw.print(bsearch(a,1,n,nextInt())+" ");
}

pw.flush();
pw.close();
br.close();
sc.close();


    }

    private static int bsearch(int[] a, int l, int r, int v) {
  if(l>r)return -1;
  if(l==r){
      if(a[l]==v)return l;
      else return -1;
  }
  int mid=(l+r)/2;
  if(a[mid]>=v)return bsearch(a,l,mid,v);
  else return bsearch(a,mid+1,r,v);


    }


}

区域查找

找左边界模板(以找最后个小于的数为例)

     int l = 0, r = n - 1;
        while(l < r) // 二分找到a数组最后一个小于b[i]的下标
        {
            int mid = l + r+1 >> 1;//这个非常重要!!!
            if(a[mid] < b[i]) l = mid; // 根据题目条件,不能取等
            else r = mid - 1;
            printf("%d %d %d \n",l,r,mid);
        }

        if(a[l] >= b[i]) l --; // 未找到小于b[i]的数,将边界左移

其中切记mid必须要为:mid=l+r+1>>1

如果mid=l+r>>1的话

在l=1,r=2将永远无法退出循环,更别说达到用二分优化时间复杂度的效果了

 找右边界模板(以找第一个大于的数为例)

l = 0, r = n - 1;
        while(l < r)
        {
            int mid = l + r >> 1;
            if(c[mid] > b[i]) r = mid;//重中之重!!!
            else l = mid + 1;
           printf("%d %d %d \n",l,r,mid);  
            
        }

        if(c[l] <= b[i]) l ++ ; // 没找到大于的也同理

其中切记mid必须要为:mid=l+r>>1

如果mid=l+r+1>>1的话

在l=0,r=1将永远无法退出循环,更别说达到用二分优化时间复杂度的效果了

例题:递增三元组

给定三个整数数组

A=[A1,A2,…AN]A=[A1,A2,…AN],
B=[B1,B2,…BN]B=[B1,B2,…BN],
C=[C1,C2,…CN]C=[C1,C2,…CN],

请你统计有多少个三元组 (i,j,k)(i,j,k) 满足:

  1. 1≤i,j,k≤N1≤i,j,k≤N
  2. Ai<Bj<CkAi<Bj<Ck

输入格式

第一行包含一个整数 NN。

第二行包含 NN 个整数 A1,A2,…ANA1,A2,…AN。

第三行包含 NN 个整数 B1,B2,…BNB1,B2,…BN。

第四行包含 NN 个整数 C1,C2,…CNC1,C2,…CN。

输出格式

一个整数表示答案。

数据范围

1≤N≤1051≤N≤105,
0≤Ai,Bi,Ci≤1050≤Ai,Bi,Ci≤105

输入样例:

3
1 1 1
2 2 2
3 3 3

输出样例:

27

1.排序加二分法

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n;
int a[N], b[N], c[N];

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    for(int i = 0; i < n; i ++ ) scanf("%d", &b[i]);
    for(int i = 0; i < n; i ++ ) scanf("%d", &c[i]);

    sort(a, a + n); // 是从a[0]开始的
    sort(b, b + n);
    sort(c, c + n);

    LL res = 0; // 防止爆int(10^5*10^5 > 1e9)
    for(int i = 0; i < n; i ++ ) // 是对于每一个b[i]来说的
    {
        int l = 0, r = n - 1;
        while(l < r) // 二分找到a数组最后一个小于b[i]的下标
        {
            int mid = l + r+1 >> 1;
            if(a[mid] < b[i]) l = mid; // 根据题目条件,不能取等
            else r = mid - 1;
          //  printf("%d %d %d \n",l,r,mid);
        }

        if(a[l] >= b[i]) l --; // 未找到小于b[i]的数,将边界左移
        // 是为了方便后面算res时小于b[i]那部分直接为0,所以对于当前的b[i]就没有满足的res(没找到肯定为0啊)
        int ans_1 = l;

        l = 0, r = n - 1;
        while(l < r)
        {
            int mid = l + r >> 1;
            if(c[mid] > b[i]) r = mid;
            else l = mid + 1;
           printf("%d %d %d \n",l,r,mid);  
            
        }

        if(c[l] <= b[i]) l ++ ; // 没找到大于的也同理
        int ans_2 = l;
        res += (LL)(ans_1 + 1) * (n - ans_2); // n - l 表示c中大于b[i]的数量
        // 因为都是从0开始的,所以ans_1要加1,而后面公式n-ans_2可以举例子验证
    }

    printf("%lld\n", res);

    return 0;
}

2.前缀和

import  java.util.*;

public class Main
{
    static int N=100005,n,i,j,a[]=new int [N],b[]=new int [N],c[]=new int [N],as[]=new int[N],cs[]=new int [N];
    static long res;
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        for(i=0;i<n;++i)a[i]=sc.nextInt();
        for(i=0;i<n;++i)b[i]=sc.nextInt();
        for(i=0;i<n;++i)c[i]=sc.nextInt();

        for(i=0;i<n;++i)as[a[i]]++;
        for(i=0;i<n;++i)cs[c[i]]++;

        for(i=1;i<N;++i)as[i]+=as[i-1];
        for(i=1;i<N;++i)cs[i]+=cs[i-1];

        for(i=0;i<n;++i){
            if(b[i]==0)continue;
            res+=(long)as[b[i]-1]*(long)(cs[N-1]-cs[b[i]]);
        }

        System.out.println(res);
    }
}

3.暴力

import java.io.*;
import java.util.*;
public class Main {
  static Scanner sc=new Scanner(System.in);
  static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) {
     int n= sc.nextInt();
int[] a=new int[n];
int[] b=new int[n];
int[] c=new int[n];
for(int i=0;i<n;i++)a[i]= sc.nextInt();
for(int i=0;i<n;i++)b[i]= sc.nextInt();
for(int i=0;i<n;i++)c[i]= sc.nextInt();
Arrays.sort(a);
Arrays.sort(b);
Arrays.sort(c);
int sum=0;
int fla=0;
for(int i=0;i<n;i++){

    for(int j=0;j<n;j++){  fla=0;
        for(int k=0;k<n;k++){
            if(b[j]>a[i]&&c[k]>b[j]){
                sum+=(n-k);
                fla=1;
                break;
            }
if(fla==1)break;

        }

    }


}


        System.out.println(sum);

    }


}

习题

P1024 [NOIP2001 提高组] 一元三次方程求解

题目描述

有形如:a x^3 + b x^2 + c x + d = 0ax3+bx2+cx+d=0 这样的一个一元三次方程。给出该方程中各项的系数(a,b,c,da,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在 -100−100 至 100100 之间),且根与根之差的绝对值 \ge 1≥1。要求由小到大依次在同一行输出这三个实根(根与根之间留有空格),并精确到小数点后 22 位。

提示:记方程 f(x) = 0f(x)=0,若存在 22 个数 x_1x1​ 和 x_2x2​,且 x_1 < x_2x1​<x2​,f(x_1) \times f(x_2) < 0f(x1​)×f(x2​)<0,则在 (x_1, x_2)(x1​,x2​) 之间一定有一个根。

输入格式

一行,44 个实数 a, b, c, da,b,c,d。

输出格式

一行,33 个实根,从小到大输出,并精确到小数点后 22 位。

输入输出样例

输入 #1复制

1 -5 -4 20

输出 #1复制

-2.00 2.00 5.00

说明/提示

【题目来源】

NOIP 2001 提高组第一题

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
import java.io.*;
import java.math.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
//int 2147483647   10
    //-2147483648
    //9223372036854775807   19
    //-9223372036854775808
    //1.7976931348623157E308  308
    //4.9E-324
    static double a,b,c,d;
    static int m;
    static long mm;
    static int n;
    static long nn;
    static ArrayList<Integer> al = new ArrayList<>();
    static Stack<Integer> sta = new Stack<>();
    static Queue<Integer> q = new LinkedList<>();
    static HashMap<Integer, String> hm = new HashMap<>();
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
   static StreamTokenizer st=new StreamTokenizer(br);
    static Scanner sc = new Scanner(new InputStreamReader(System.in));
    public static double nextDouble()throws Exception{
        st.nextToken();
        return st.nval;
    }
    public static PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out));
    public static double sum(double x){
        return a*x*x*x+b*x*x+c*x+d;
    }
    public static void main(String[] args) throws Exception {
        double l,r,m,x1,x2;
        int s=0,i;
        a= sc.nextDouble();
        b= sc.nextDouble();
        c= sc.nextDouble();
        d= sc.nextDouble();
      for( i=-100;i<=100;i++){
          l=i;
          r=i+1;
          x1=sum(l);
          x2=sum(r);
          if(x1==0){
              pw.printf("%.2f ",l);
          s++;
          }
         if(x1*x2<0){
             while(r-l>=0.001){
                 m=(l+r)/2;
                 if(sum(m)*sum(r)<=0){
                     l=m;

                 }else r=m;


             }
             pw.printf("%.2f ",l);

             s++;
             if(s==3)break;
          //   System.out.println(s);

         }









      }












pw.flush();
        //bw.write("");
        bw.flush();
        sc.close();
    }
}

P1163 银行贷款

题目描述

当一个人从银行贷款后,在一段时间内他(她)将不得不每月偿还固定的分期付款。这个问题要求计算出贷款者向银行支付的利率。假设利率按月累计。

输入格式

三个用空格隔开的正整数。

第一个整数表示贷款的原值,第二个整数表示每月支付的分期付款金额,第三个整数表示分期付款还清贷款所需的总月数。

输出格式

一个实数,表示该贷款的月利率(用百分数表示),四舍五入精确到 0.1\%0.1%。

输入输出样例

输入 #1复制

1000 100 12

输出 #1复制

2.9

说明/提示

数据保证,1 ≤ 贷款的原值,分期付款金额 ≤2(31次方)−1,1≤ 月数 ≤3000。

用到秦九韶算法和二分法

秦九韶算法

import java.util.Scanner;
 
public class qjshao {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入x:");
        double x = in.nextDouble();
        System.out.println("请输入k:");
        int k = in.nextInt();
        double[] a = new double[k];
        System.out.println("请输入系数a(从a0开始):");
        for (int i = 0; i < a.length; i++) {
            a[i] = in.nextDouble();
        }
 
        System.out.println("多项式结果为"+qjshao(k, a, x));
    }
 
    static double qjshao(int k, double a[], double x) {
        double res = a[k-1];
        for (int i=1;i<k;i++)
            res = a[k-i-1] + x * res;
        return res;
    }
}

import java.*;
import java.io.*;
import java.util.Scanner;

public class Main {
    static Scanner sc=new Scanner(System.in);
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st=new StreamTokenizer(br);
    private static double nextDouble()throws Exception{
        st.nextToken();
        return st.nval;
    }
    private static int nextInt()throws Exception{
        st.nextToken();
        return (int)st.nval;

    }
    static PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out));
    private static double a,b;
    private static int c;
private static void getresult(double left,double right){
    if(right-left<0.0001){
        pw.printf("%.1f",left*100);
        return;
    }
    double mid=(left+right)/2,temp=a;
    for(int i=0;i<c;i++){
       temp=temp*(1+mid)-b;
    }
    if(temp>0)getresult(left,mid);
    else if(temp<0)getresult(mid,right);
    else pw.printf("%.1f",mid*100);


}






    public static void main(String[] args)throws Exception {
        a=nextDouble();
        b=nextDouble();
       c=nextInt();
        getresult(0,5);
        pw.flush();

    }


}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀化第一深情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值