You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.2 KiB
59 lines
1.2 KiB
package class14;
|
|
|
|
import java.util.Comparator;
|
|
import java.util.PriorityQueue;
|
|
|
|
public class Code04_IPO {
|
|
|
|
// 最多K个项目
|
|
// W是初始资金
|
|
// Profits[] Capital[] 一定等长
|
|
// 返回最终最大的资金
|
|
public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) {
|
|
PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator());
|
|
PriorityQueue<Program> maxProfitQ = new PriorityQueue<>(new MaxProfitComparator());
|
|
for (int i = 0; i < Profits.length; i++) {
|
|
minCostQ.add(new Program(Profits[i], Capital[i]));
|
|
}
|
|
for (int i = 0; i < K; i++) {
|
|
while (!minCostQ.isEmpty() && minCostQ.peek().c <= W) {
|
|
maxProfitQ.add(minCostQ.poll());
|
|
}
|
|
if (maxProfitQ.isEmpty()) {
|
|
return W;
|
|
}
|
|
W += maxProfitQ.poll().p;
|
|
}
|
|
return W;
|
|
}
|
|
|
|
public static class Program {
|
|
public int p;
|
|
public int c;
|
|
|
|
public Program(int p, int c) {
|
|
this.p = p;
|
|
this.c = c;
|
|
}
|
|
}
|
|
|
|
public static class MinCostComparator implements Comparator<Program> {
|
|
|
|
@Override
|
|
public int compare(Program o1, Program o2) {
|
|
return o1.c - o2.c;
|
|
}
|
|
|
|
}
|
|
|
|
public static class MaxProfitComparator implements Comparator<Program> {
|
|
|
|
@Override
|
|
public int compare(Program o1, Program o2) {
|
|
return o2.p - o1.p;
|
|
}
|
|
|
|
}
|
|
|
|
}
|