你最後獲得的最大錢數

輸入: 參數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");
		}
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章