AcWing 789. 数的范围——整数二分法JAVA
原题链接
简单
作者:
珍妮玛饰哆
,
2021-07-22 10:15:41
,
所有人可见
,
阅读 246
JAVA样板
import java.util.*;
public class Main
{
static int N = 10010, n ,m;
static int[] q = new int [N];
public static void main(String[] args) {
Scanner sy = new Scanner(System.in);
n = sy.nextInt(); m = sy.nextInt();
for(int i = 0;i<n;i++) q[i] = sy.nextInt();
while(m--!=0)
{
int a = sy.nextInt();
int l = 0 , r = n-1;
while( r > l)
{
int mid = r + l >> 1;
if(q[mid] >= a) r = mid;
else l = mid + 1;
}
if( q[l] == a)
{
System.out.print(l + " ");
r = n-1;
while( l < r)
{
int mid = r + l + 1 >> 1;
if(q[mid] <= a) l = mid;
else r = mid - 1;
}
if( q[r] == a) System.out.print(r);
}
else System.out.print("-1 -1");
System.out.println();
}
}
}