起床晚了10:20才发现有笔试第一题15分钟ak第二题做了一个半小时,一开始的做法时间复杂度爆了,只过24%,改进的代码如下,样例能够全过,自己也测试了别的数据,为什么实际用例送我大0蛋啊真的红温了package pdd_2;import java.util.*;// 注意类名必须为 Main, 不要有任何 package xxx 信息public class Main_1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int group = in.nextInt(); int[] res = new int[group]; for (int i = 0; i int count = in.nextInt(); int[] nums = new int[count]; double avg = 0; for (int j = 0; j nums[j] = in.nextInt(); avg = avg + (double) nums[j]; } avg = avg / count; Arrays.sort(nums); Set> list = new HashSet<>(); Set> have = new HashSet<>(); getRes(nums, 0, nums.length - 1, avg, list, have); res[i] = list.size(); } for (int i = 0; i System.out.println(res[i]); } } public static void getRes(int[] nums, int indexStart, int indexEnd, double avg, Set> list, Set> have) { if (indexStart == nums.length || indexEnd == -1 || indexStart >= indexEnd) { return; } Set set = new HashSet<>(); set.add(indexStart); set.add(indexEnd); if (have.contains(set)) { return; } have.add(set); if (nums[indexStart] + nums[indexEnd] == avg * 2) { list.add(set); int x = 1; while (indexStart + x x++; } int y = 1; while (indexStart y++; } for (int i = 1; i for (int j = 1; j Set s = new HashSet<>(); s.add(indexStart + i); s.add(indexEnd - j); if (s.size() == 2) { list.add(s); } } } getRes(nums, indexStart + x, indexEnd - y, avg, list, have); } if (nums[indexStart] + nums[indexEnd] > avg * 2) { getRes(nums, indexStart, indexEnd - 1, avg, list, have); } else { getRes(nums, indexStart + 1, indexEnd, avg, list, have); } }}