import java.util.;
import java.io.;
public class Main{
public static void main(String[]args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] nums1 = br.readLine().split(” “);
int n = Integer.parseInt(nums1[0]);
int q = Integer.parseInt(nums1[1]);
String[] nums2 = br.readLine().split(” “);
int[] nums = new int[n];
for(int i = 0;i < n;i){
nums[i] = Integer.parseInt(nums2[i]);
}
for(int j = 0;j < q;j){
int target = Integer.parseInt(br.readLine());
Binaryleft(target,nums);
Binaryright(target,nums);
}
}
public static void Binaryleft(int target,int[]nums){
int left = 0;
int right = nums.length - 1;
while(left <= right){
int mid = left + (right - left) / 2;
if(nums[mid] == target){
right = mid - 1;
}else if(nums[mid] < target){
left = mid + 1;
}else if(nums[mid] > target){
right = mid - 1;
}
}
if(left < 0 || left >= nums.length){
System.out.print(“-1 “);
}else if(nums[left] == target){
System.out.print(left + ” “);
}else{
System.out.print(“-1 “);
}
}
public static void Binaryright(int target,int[]nums){
int left = 0;
int right = nums.length - 1;
while(left <= right){
int mid = left + (right - left) / 2;
if(nums[mid] == target){
left = mid + 1;
}else if(nums[mid] < target){
left = mid + 1;
}else if(nums[mid] > target){
right = mid - 1;
}
}
if(left < 0 || right >= nums.length){
System.out.println(“-1”);
}else if(nums[right] == target){
System.out.println(right);
}else{
System.out.println(“-1”);
}
}
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla