📌  相关文章
📜  最大化 N 个项目的利润总和,使得第 i 个项目的利润是其重量和不同选择值计数的乘积

📅  最后修改于: 2022-05-13 01:56:08.307000             🧑  作者: Mango

最大化 N 个项目的利润总和,使得第 i 个项目的利润是其重量和不同选择值计数的乘积

给定一个由N对项目组成的数组arr[]作为{value, weight} ,任务是通过选择所有给定的N 个项目来找到最大的利润总和,使得选择第i项目的利润计算为乘积它的权重和已经采用的不同值的数量。

例子:

方法:给定的问题可以通过使用贪心方法来解决。这个想法是根据项目的权重对给定的数组arr[]进行排序,首先选择所有不同的项目,然后选择剩余的项目。请按照以下步骤解决给定的问题:

  • 初始化一个集合,比如items ,以存储所有唯一的项目。
  • 初始化一个变量,比如uniqCnt = 0 ,以存储唯一项的计数。
  • 初始化一个变量,比如maxProfit = 0 ,以存储总最大利润。
  • 初始化一个变量,比如totWeight = 0 ,以存储不唯一的项目的总重量。
  • 遍历数组arr[]并将所有唯一项存储到 set items 中
  • 按权重的升序对数组arr[]进行排序。
  • 将唯一元素的计数存储为uniqCnt = items.size()并从项目中删除所有元素。
  • 遍历给定的数组arr[]并检查以下条件:
    • if(!items.count(arr[i].first))如果发现为真,则将arr[i].first元素插入到设置的项目中并计算利润并通过maxProfit +=将其更新为maxProfit items.size() * arr[i].second
    • 否则,将totWeight更新为totWeight += arr[i].second
  • 计算非唯一项目的利润并将maxProfit更新为maxProfit += totWeight * uniqCnt
  • 完成上述步骤后,打印maxProfit的值作为所得的最大利润。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Comparator function to sort vector
// with respect to the second value
bool comp(pair a, pair b)
{
    if (a.second > b.second)
        return false;
 
    return true;
}
 
// Function to maximize the total profit
// by choosing  the array of items in a
// particular order
int maxProfit(vector >& arr,
              int N)
{
 
    // Stores the unique items
    set items;
 
    for (int i = 0; i < N; i++) {
 
        // Store all the unique items
        items.insert(arr[i].first);
    }
 
    // Sort the arr with respect to
    // the weights
    sort(arr.begin(), arr.end(), comp);
 
    // Stores the number of unique
    // items count
    int uniqCnt = items.size();
 
    // Clear the set items
    items.clear();
 
    // Stores the maximum profit
    int maxProfit = 0;
 
    // Stores the total weight of items
    // which are not unique
    int totWeight = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Check the current item is unique
        if (!items.count(arr[i].first)) {
 
            // Insert the current item
            // into the items set
            items.insert(arr[i].first);
 
            // Calculate the profit for
            // the current item and
            // update the maxProfit
            maxProfit += items.size() * arr[i].second;
        }
        else
            // Update the totWeight by
            // adding current item weight
            totWeight += arr[i].second;
    }
 
    // Update the maxProfit by calculating
    // the profit for items which are not
    // unique and adding it to maxProfit
    maxProfit += totWeight * uniqCnt;
 
    // Return maxProfit
    return maxProfit;
}
 
// Driver Code
int main()
{
    vector > arr{
        { 1, 4 }, { 2, 3 }, { 1, 2 }, { 3, 2 }
    };
    int N = arr.size();
    cout << maxProfit(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
    static class pair
    {
        int first, second;
        public pair(int first, int second) 
        {
            this.first = first;
            this.second = second;
        }   
    }
// Comparator function to sort vector
// with respect to the second value
boolean comp(pair a, pair b)
{
    if (a.second > b.second)
        return false;
 
    return true;
}
 
// Function to maximize the total profit
// by choosing  the array of items in a
// particular order
static int maxProfit(pair[] arr,
              int N)
{
 
    // Stores the unique items
    HashSet items = new HashSet();
 
    for (int i = 0; i < N; i++) {
 
        // Store all the unique items
        items.add(arr[i].first);
    }
 
    // Sort the arr with respect to
    // the weights
    Arrays.sort(arr,(a,b)->a.second-b.second);
 
    // Stores the number of unique
    // items count
    int uniqCnt = items.size();
 
    // Clear the set items
    items.clear();
 
    // Stores the maximum profit
    int maxProfit = 0;
 
    // Stores the total weight of items
    // which are not unique
    int totWeight = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Check the current item is unique
        if (!items.contains(arr[i].first)) {
 
            // Insert the current item
            // into the items set
            items.add(arr[i].first);
 
            // Calculate the profit for
            // the current item and
            // update the maxProfit
            maxProfit += items.size() * arr[i].second;
        }
        else
            // Update the totWeight by
            // adding current item weight
            totWeight += arr[i].second;
    }
 
    // Update the maxProfit by calculating
    // the profit for items which are not
    // unique and adding it to maxProfit
    maxProfit += totWeight * uniqCnt;
 
    // Return maxProfit
    return maxProfit;
}
 
// Driver Code
public static void main(String[] args)
{
    pair[] arr = {
            new pair( 1, 4 ), new pair( 2, 3 ), new pair( 1, 2 ), new pair( 3, 2 )
    };
    int N = arr.length;
    System.out.print(maxProfit(arr, N));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python Program to implement
# the above approach
 
# Function to maximize the total profit
# by choosing  the array of items in a
# particular order
def maxProfit(arr, N):
 
    # Stores the unique items
    items = set()
 
    for i in range(N):
 
        # Store all the unique items
        items.add(arr[i]["first"])
 
    # Sort the arr with respect to
    # the weights
 
    arr = sorted(arr, key=lambda i: i['second'])
 
    # Stores the number of unique
    # items count
    uniqCnt = len(items)
 
    # Clear the set items
    items.clear()
 
    # Stores the maximum profit
    maxProfit = 0
 
    # Stores the total weight of items
    # which are not unique
    totWeight = 0
 
    for i in range(0, N):
 
        # Check the current item is unique
        if (not (arr[i]['first']) in items):
 
            # Insert the current item
            # into the items set
            items.add(arr[i]['first'])
 
            # Calculate the profit for
            # the current item and
            # update the maxProfit
            maxProfit += len(items) * arr[i]['second']
 
        else:
            # Update the totWeight by
            # adding current item weight
            totWeight += arr[i]['second']
 
    # Update the maxProfit by calculating
    # the profit for items which are not
    # unique and adding it to maxProfit
    maxProfit += totWeight * uniqCnt
 
    # Return maxProfit
    return maxProfit
 
 
# Driver Code
arr = [
    {"first": 1, "second": 4},
    {"first": 2, "second": 3},
    {"first": 1, "second": 2},
    {"first": 3, "second": 2}
]
N = len(arr)
print(maxProfit(arr, N))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
    public class pair : IComparable
    {
        public int first,second;
        public pair(int first, int second)
        {
            this.first = first;
            this.second = second; 
        }
 
        // Comparator function to sort vector
        // with respect to the second value
        public int CompareTo(pair p)
        {
            return this.second - p.second;
        }
    }
 
// Function to maximize the total profit
// by choosing  the array of items in a
// particular order
static int maxProfit(pair[] arr,
              int N)
{
 
    // Stores the unique items
    HashSet items = new HashSet();
 
    for (int i = 0; i < N; i++) {
 
        // Store all the unique items
        items.Add(arr[i].first);
    }   
 
    // Sort the arr with respect to
    // the weights
    Array.Sort(arr);
 
    // Stores the number of unique
    // items count
    int uniqCnt = items.Count;
 
    // Clear the set items
    items.Clear();
 
    // Stores the maximum profit
    int maxProfit = 0;
 
    // Stores the total weight of items
    // which are not unique
    int totWeight = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Check the current item is unique
        if (!items.Contains(arr[i].first)) {
 
            // Insert the current item
            // into the items set
            items.Add(arr[i].first);
 
            // Calculate the profit for
            // the current item and
            // update the maxProfit
            maxProfit += items.Count * arr[i].second;
        }
        else
            // Update the totWeight by
            // adding current item weight
            totWeight += arr[i].second;
    }
 
    // Update the maxProfit by calculating
    // the profit for items which are not
    // unique and adding it to maxProfit
    maxProfit += totWeight * uniqCnt;
 
    // Return maxProfit
    return maxProfit;
}
 
// Driver Code
public static void Main(String[] args)
{
    pair[] arr = {
            new pair( 1, 4 ), new pair( 2, 3 ), new pair( 1, 2 ), new pair( 3, 2 )
    };
    int N = arr.Length;
    Console.Write(maxProfit(arr, N));
 
}
}
 
// This code is contributed by shikhasingrajput


Javascript



输出:
27

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