有大佬看看为什么答案错误,只过了 45 %,都没超时 public class Main { static long res = 0; static long[] nums; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); nums = new long[n]; for (int i = 0; i < n; i++) { nums[i] = scanner.nextLong(); } Arrays.sort(nums); backtrack(nums.length, 0, 0, 0, 0, k); System.out.println(res); } private static void backtrack(long n, int cur, long sum, long total, long maxWeight, long k) { if (total != 0) { if (maxWeight * total <= sum * k) { res = Math.max(res, total); } else { return; } } if (cur == n) { return; } backtrack(n, cur + 1, sum + nums[cur], total + 1, Math.max(maxWeight, nums[cur]), k); backtrack(n, cur + 1, sum, total, maxWeight, k); } }
点赞 11

相关推荐

点赞 评论 收藏
分享
11-09 11:01
济南大学 Java
Java抽象带篮子:外卖项目真得美化一下,可以看看我的详细的外卖话术帖子
点赞 评论 收藏
分享
牛客网
牛客企业服务