import java.util.Scanner; // 4110:圣诞老人的礼物-Santa Clau’s Gifts public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int maxW = input.nextInt(); int[][] p = new int[n][2]; double[][] density = new double[n][2]; // 获得输入并计算密度 for (int i = 0; i < n; i++) { p[i][0] = input.nextInt(); p[i][1] = input.nextInt(); density[i][0] = i; density[i][1] = 1.0 * p[i][0] / p[i][1]; } // 对密度进行排序 for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { double[] swap = new double[2]; if (density[i][1] < density[j][1]) { swap = density[i]; density[i] = density[j]; density[j] = swap; } } } int sumW = 0; double value = 0; for (int i = 0; i < n; i++) { int index = (int) density[i][0]; if (sumW >= maxW) { break; } if (p[index][1] + sumW <= maxW) { sumW += p[index][1]; value += p[index][0]; } else { value += ((maxW - sumW) * density[i][1]); sumW = maxW; } } System.out.println(value); } }
时间: 2024-10-10 14:00:19