1 题目
2 代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class B1020 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] oneLine = br.readLine().split("\\s");
String[] twoLine = br.readLine().split("\\s");
String[] thrLine = br.readLine().split("\\s");
double maxGain = 0;
int N = Integer.valueOf(oneLine[0]);
double D = Double.valueOf(oneLine[1]);
ArrayList<MoonCake> moonCakes = new ArrayList<>(); // 保存所有的月饼种类、总库存以及总价格
for (int i = 0; i < N; i++) {
moonCakes.add(new MoonCake(Double.valueOf(twoLine[i]), Double.valueOf(thrLine[i])));
}
// 按照单价给月饼从大到小排序
Collections.sort(moonCakes, new Comparator<MoonCake>() {
@Override
public int compare(MoonCake o1, MoonCake o2) {
if ((o2.prePrice - o1.prePrice) > 0)
return 1;
else return -1;
}
});
// 从价格高的月饼起售显然可以获得最大的收益
for (int i = 0; i < moonCakes.size(); i++) {
if (moonCakes.get(i).number <= D) {
maxGain += moonCakes.get(i).totalPrice;
D -= moonCakes.get(i).number;
} else {
maxGain += D * moonCakes.get(i).prePrice;
break;
}
if (D == 0)
break;
}
// 按要求输出即可
System.out.println(String.format("%.2f", maxGain));
}
}
class MoonCake {
double number; // 月饼库存量
double totalPrice; // 总售价
double prePrice; // 单价
public MoonCake(double number, double totalPrice) {
this.number = number;
this.totalPrice = totalPrice;
this.prePrice = totalPrice / number;
}
}
3 要点
(1)题目虽然没有说库存、总售价为double类型,不过建议首选double类型存储,其中测试2好像就是测试库存不为整数。