输入: 参数1,正数数组costs
参数2,正数数组profits
参数3,正数k
参数4,正数m
costs[i]表示i号项目的花费; profits[i]表示i号项目在扣除花费之后还能挣到的钱(利润) ;k表示你不能并行、只能串行的最多做k个项目; m表示你初始的资金
说明:你每做完一个项目,马上获得的收益,可以支持你去做下一个项目。
输出: 你最后获得的最大钱数
思路:这个题是个标准的贪心问题,先把所有项目按照花费组成一个小根堆,然后按照收益组成一个大根堆,启动组件记为C_init,小根堆中只要小于C_init花费的,就从小根堆中弹出进大根堆,当一个项目从小根堆拿出来进大根堆的时候,代表这个项目可以考虑了。小根堆一直弹出直到堆顶大于启动资金的时候,表示这个项目已经做不了了。把所有启动资金小于给定初始资金的项目全部弹到大根堆里面,表示哪些项目是可以考虑的。然后选择大根堆堆顶的项目做。做完之后,初始资金会增加,导致小根堆中的某些项目又可以进大根堆,以此类推。一共选k个。
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import tool.MyArraysTool;
public class IPO {
public static class Project {
int cost;
int profit;
public Project(int cost, int profit) {
this.cost = cost;
this.profit = profit;
}
}
public static int ipo(int[] costs, int[] profits, int k, int m) {
if (costs == null || profits == null) {
return m;
}
// 按照cost构造项目对象-小根堆
PriorityQueue<Project> minCostsQueue = new PriorityQueue<Project>(new MinCostComparator());
// 按照profits排序的大根堆
PriorityQueue<Project> maxProfitsQueue = new PriorityQueue<Project>(new MaxProfitsComparator());
// 根据两个数组构造项目对象--并使用小根堆保存
for (int i = 0; i < costs.length; i++) {
minCostsQueue.add(new Project(costs[i], profits[i]));
}
// 开始做k次项目
for (int num = 0; num < k; num++) {
// 每次做项目开始,都按照cost和当前m的值,将能目前能做的项目解锁,放到按照收益排序的大根堆profitsQueue里
while (!minCostsQueue.isEmpty() && minCostsQueue.peek().cost <= m) {
maxProfitsQueue.add(minCostsQueue.poll());
}
if (!maxProfitsQueue.isEmpty()) {// 从大根堆里取出当前能做的(解锁的)项目中收益最大的项目做
m = m + maxProfitsQueue.poll().profit;
}
}
return m;
}
// 实现按照利润由大到小的比较器,大根堆比较器
public static class MaxProfitsComparator implements Comparator<Project> {
@Override
public int compare(Project o1, Project o2) {
return o2.profit - o1.profit;
}
}
// 实现按照花费由小到大的比较器,小根堆比较器
public static class MinCostComparator implements Comparator<Project> {
@Override
public int compare(Project o1, Project o2) {
return o1.cost - o2.cost;
}
}
/**
* 对数器,用来验证ipo的正确性
*/
public static int ipo1(int[] costs, int[] profits, int k, int m) {
if (costs == null || profits == null) {
return m;
}
Queue<Project> queue = new LinkedList<Project>();
for (int i = 0; i < costs.length; i++) {
queue.add(new Project(costs[i], profits[i]));
}
// 开始做K次项目
for (int num = 0; !queue.isEmpty() && num < k; num++) {
Project maxProfitsProject = null;
for (Project project : queue) {// 找到一个合适的项目
if (project.cost <= m) {
maxProfitsProject = project;
}
}
if (maxProfitsProject == null) {
return m;
}
for (Project project : queue) {
if (project.cost <= m && maxProfitsProject.profit < project.profit) {
maxProfitsProject = project;
}
}
queue.remove(maxProfitsProject);
m += maxProfitsProject.profit;
}
return m;
}
public static void main(String[] args) {
int i = 0;
while (i < 300000) {
int[] costs = MyArraysTool.generatePositiveArray(10, 200);// 生成正数数组
int[] profits = MyArraysTool.generatePosLengthArray(costs.length, 200);// 生成指定长度的正数数组
int k = (int) (Math.random() * 101);
int m = (int) (Math.random() * 16);
int ipoRes = ipo(costs, profits, k, m);
int ipo1Res = ipo1(costs, profits, k, m);
if(ipoRes != ipo1Res ) {
System.out.print("ipo = "+ ipoRes +"ipo1=: "+ipo1Res);
break;
}
i++;
}
if(i == 300000) {
System.out.println("good");
}
}
}