--------------------------------------------------------------------------------------------------------------------------------------------------------
时间限制:1秒 空间限制:32768K 代码长度限制 100 KB
--------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------
(1).先将数组进行升序排序; (2).两层循环,第一层确定最小值min,第二层逐一将数组元素当成最大值max,判断是否满足条件,如果满足,继续,直到不满足,确定满足条件时最右元素的index;
(3).第二层循环可以进行优化,类似二分查找的方式,可以减少循环次数,否则容易内存超出限制;
--------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main_a1_020 { public static void main(String[] args) { List<String> list = new ArrayList<String>(); Scanner scan = new Scanner(System.in); while(scan.hasNext()){ int n = scan.nextInt(); int p = scan.nextInt(); int[] nums = new int[n]; for (int i = 0; i < nums.length; i++) { nums[i] = scan.nextInt(); } Arrays.sort(nums); findNums(nums,p); } } public static void findNums(int[] nums,int p){ int min = -1,max = -1; int count = 0; for (int i = 0; i < nums.length; i++) { if(count >= (nums.length - i)){ break; } int countTemp = 0; min = nums[i]; int ji = min*p; int left = i,right = nums.length-1; while(true){ int index = (left+right)/2; max = nums[index]; if(max <= ji){ countTemp = index - i + 1; left = index + 1; }else{ right = index - 1; } if(left > right){ break; } } if(countTemp > count){ count = countTemp; } } System.out.println(count); } }