zoukankan      html  css  js  c++  java
  • 最大利润


    import java.util.Comparator;
    import java.util.PriorityQueue;

    /**
    * 输入正数数组cost表示每个项目的花费,正数数组profits表示每个项目的利润,M表示初始资金,K表示最多只能串行做k个项目
    * 返回最后获得的最大钱数
    */
    public class IPO {

    public static int findMaximizedCapital(int k, int m, int[] profits, int[] costs) {
    PriorityQueue<Program> minCostQ = new PriorityQueue<>(Comparator.comparingInt(a -> a.cost));
    PriorityQueue<Program> maxProfitQ = new PriorityQueue<>((a, b) -> b.profit - a.profit);
    for (int i = 0; i < profits.length; i++) {
    minCostQ.add(new Program(profits[i], costs[i]));
    }
    for (int i = 0; i < k; i++) {
    while (!minCostQ.isEmpty() && minCostQ.peek().cost <= m) {
    maxProfitQ.add(minCostQ.poll());
    }
    if (maxProfitQ.isEmpty()) {
    return m;
    }
    m += maxProfitQ.poll().profit;
    }
    return m;
    }

    public static class Program {

    // 纯利润
    public int profit;

    // 花费
    public int cost;

    public Program(int profit, int cost) {
    this.profit = profit;
    this.cost = cost;
    }

    }

    }

    /* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */
  • 相关阅读:
    2017.10.27
    2017.10.26
    codeforces 652 E Pursuit For Artifacts
    bzoj 1123 BLO
    bzoj 2839 集合计数
    bzoj 3170: [Tjoi 2013]松鼠聚会
    bzoj 2503 相框 欧拉回路
    bzoj 1786 [Ahoi2008]Pair 配对
    bzoj 1014 [JSOI2008]火星人prefix 哈希+splay
    csp-s74 瓶颈
  • 原文地址:https://www.cnblogs.com/laydown/p/13060977.html
Copyright © 2011-2022 走看看