请问这个二分为什么超时呀

P2249 【深基13.例1】查找

YMD18079987348 @ 2024-04-12 17:10:45


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        int m=scanner.nextInt();
        int []nums=new int[n+1];
        for (int i=1;i<=n;i++){
            nums[i]=scanner.nextInt();
        }
        for (int i=0;i<m;i++){
            int x=scanner.nextInt();
            int res=binary_search(nums,x);
            System.out.print(res+" ");
        }
    }

    public static int binary_search(int []nums,int x){
        int l=0;
        int r=nums.length;
        while(l+1<r){
            int mid=(l+r)/2;
            if(isBlue(nums[mid],x)){
                l=mid;
            } else r=mid;
        }
        if(nums[r]==x){
            return r;
        }
        return -1;
    }

    public static boolean isBlue(int num,int x){
        return num<x;
    }
}

by YMD18079987348 @ 2024-04-12 17:21:09

P2249


|