题解 | #奖学金#贪心策略
奖学金
https://www.nowcoder.com/practice/cee98a512ec246a2918ea8121f7612c8
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); int r = in.nextInt(); int avg = in.nextInt(); int[][] scores = new int[n][2]; int sum = 0; for (int i = 0; i < n; i++) { scores[i][0] = in.nextInt(); scores[i][1] = in.nextInt(); sum += scores[i][0]; } // 计算需要的总分数 int needed = avg * n - sum; if (needed <= 0) { System.out.println(0); continue; } // System.out.println(needed); //按照复习时间排序 时间越少 提升分数空间越大的在最前面 Arrays.sort(scores, (x, y)-> { return x[1]==y[1] ? x[0]-y[0] : x[1]-y[1]; }); long totalTime = 0; for (int i = 0; i < n; i++) { if (scores[i][0] < r) { //可以提升的分数 int num = r - scores[i][0]; if(num>=needed){ totalTime +=(long)needed*scores[i][1]; needed=0; }else{ totalTime+=(long)num*scores[i][1]; needed-=num; } if(needed==0){ break; } } } System.out.println(totalTime); } } }