📜  选择最多K个项目所需的最大资金量

📅  最后修改于: 2021-10-26 05:20:14             🧑  作者: Mango

给定一个整数N ,代表项目数量,两个数组P[]C[] ,由N 个整数组成,以及两个整数WK其中, W是初始资本金额, P[i]C[i]是选择第i项目所需的利润和资本。任务是计算最多选择K 个项目所需的最大资金量,从而将所选项目的利润添加到W并选择至少需要C[i] 的任何项目。

例子:

方法:给定的问题可以使用贪心算法和优先级队列来解决。请按照以下步骤解决问题:

  • 初始化一个priority_queue PQ来存储所有资金最多为W的项目的利润。
  • 使用变量i作为索引遍历数组C[]并将{C[i], i}对推入对V的向量中。
  • 相对于第一个元素按升序对向量 V 进行排序。
  • 迭代直到K大于0
    • 将优先队列PQ中所有资金最多为 W的项目的收益推送。
    • 将资本金额W增加PQ的最大元素,即W += PQ.top() ,然后弹出PQ的顶部元素。
    • K1
  • 完成以上步骤后,打印W的值作为得到的最大资本。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate maximum capital
// obtained after choosing at most K
// projects whose capital is less than
// the given cost of projects
int maximizedCapital(int K, int W,
                     vector& profits,
                     vector& capital)
{
    // Stores all projects with
    // capital at most W
    priority_queue pq;
 
    // Stores the pair of {C[i], i}
    vector > v;
 
    // Traverse the vector C[]
    for (int i = 0;
         i < capital.size(); i++) {
        v.push_back({ capital[i], i });
    }
 
    // Sort the vector v
    sort(v.begin(), v.end());
 
    int j = 0;
 
    while (K) {
 
        // If capital is at most W
        while (j < (int)capital.size()
               && v[j].first <= W) {
 
            // Push the profit into
            // priority queue
            pq.push(profits[v[j].second]);
 
            // Increment j by one
            j++;
        }
 
        // If pq is not empty
        if (!pq.empty()) {
 
            // Update the capital W
            W = W + pq.top();
 
            // Delete the top of pq
            pq.pop();
        }
 
        // Decrement K by one
        K--;
    }
 
    // Return the maximum capital
    return W;
}
 
// Driver Code
int main()
{
    int K = 2;
    int W = 0;
    vector P = { 1, 2, 3 };
    vector C = { 0, 1, 1 };
    cout << maximizedCapital(K, W, P, C);
 
    return 0;
}


Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to calculate maximum capital
    // obtained after choosing at most K
    // projects whose capital is less than
    // the given cost of projects
    static int maximizedCapital(int K, int W, int profits[],
                                int capital[])
    {
        // Stores all projects with
        // capital at most W
        PriorityQueue pq = new PriorityQueue<>(
            Collections.reverseOrder());
 
        // Stores the pair of {C[i], i}
        ArrayList v = new ArrayList<>();
 
        // Traverse the vector C[]
        for (int i = 0; i < capital.length; i++) {
            v.add(new int[] { capital[i], i });
        }
 
        // Sort the vector v
        Collections.sort(v, (a, b) -> {
            if (a[0] != b[0])
                return a[0] - b[0];
            return a[1] - b[1];
        });
 
        int j = 0;
 
        while (K != 0) {
 
            // If capital is at most W
            while (j < capital.length && v.get(j)[0] <= W) {
 
                // Add the profit into
                // priority queue
                pq.add(profits[v.get(j)[1]]);
 
                // Increment j by one
                j++;
            }
 
            // If pq is not empty
            if (!pq.isEmpty()) {
 
                // Update the capital W
                W = W + pq.peek();
 
                // Delete the top of pq
                pq.poll();
            }
 
            // Decrement K by one
            K--;
        }
 
        // Return the maximum capital
        return W;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int K = 2;
        int W = 0;
        int P[] = { 1, 2, 3 };
        int C[] = { 0, 1, 1 };
       
        System.out.println(maximizedCapital(K, W, P, C));
    }
}
 
// This code is contributed by Kingash.


输出:
4

时间复杂度: O(N * K * log N)
辅助空间: O(N)