算法 BISHI49 小红闯关 思路 求解代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); String[] str = br.readLine().split("\\s+"); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); String[] numStr = br.readLine().split("\\s+"); long[] a = new long[n]; long total = 0; for (int i = 0; i < n; i++) { a[i] = Long.parseLong(numStr[i]); total += a[i];// 累加总耗时 } // 初始化最大堆(优先队列),用于存储可跳过的关卡耗时(优先弹出最大值) // PriorityQueue默认是最小堆,Collections.reverseOrder()转为最大堆 PriorityQueue<Long> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); long saveTime = 0; for (int i = n - 1; i >= 0; i--) { // (i+1)表示"已通过前i+1个关卡"(按顺序),满足每k个关卡则获得1个道具 // 0-based索引转1-based计数,确保规则匹配 if ((i + 1) % k == 0 && !maxHeap.isEmpty()) { // 获得道具,跳过堆中耗时最大的关卡,累加跳过的耗时 saveTime += maxHeap.poll(); } // 最小总耗时 = 全通关耗时 - 跳过的最大耗时之和 maxHeap.add(a[i]); } out.println(total - saveTime); out.flush(); out.close(); br.close(); }