没人写题解嘛,暴力法,枚举全部情况,放入容器中 65%
然后二分法(借鉴抄袭大佬的),不知道具体的值,也能用二分,具体的看代码吧。
import java.util.*; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(), k = scanner.nextInt(); Integer[] ints = new Integer[n]; for(int i = 0; i < n; i++){ ints[i] = scanner.nextInt(); } // //暴力法 65% // ArrayList<Integer> arrayList = new ArrayList<>(); // for(int i = 0; i < ints.length; i++){ // for(int j = i + 1; j < ints.length; j++){ // arrayList.add(ints[i] * ints[j]); // } // } // arrayList.sort((i, j) -> Integer.compare(j, i)); // System.out.println(arrayList.get(k - 1)); // // // } Arrays.sort(ints); long minP = 0, maxP = ints[n - 1] * ints[n - 2]; while (minP < maxP) { long mid = (minP + maxP + 1) >> 1; long cnt = 0; int low = 0, high = n - 1; //找出比mid大的数 while (low < high && cnt < k) { while (low < high && ints[low] * ints[high] < mid) { ++low; } cnt += Math.max(high - low, 0); --high; } //剪枝 if(cnt >= k){ minP = mid; }else{ maxP = mid - 1; } } System.out.println(minP); } }