📜  最大化旅行团的幸福感

📅  最后修改于: 2021-09-17 07:46:58             🧑  作者: Mango

将在字节之城 ByteLand 组织一次神秘的土地之旅。不幸的是,座位有限,比如A ,有N组人。每组可以有老人o ,孩子c ,男人m和女人w 。组委会希望将旅行的幸福价值最大化。旅行的幸福值是所有要去的团体的幸福值的总和。如果每个成员都能得到一个座位,那么一个小组就会去旅行(打破一个小组不是一件好事)。

  1. 孩子的幸福c = 4
  2. 女人的幸福w = 3
  3. 人的幸福m = 2
  4. 老人的幸福度o = 1

G组的幸福度,H(G)=(组内人的幸福度总和)*(组内人数)。
小组的幸福度 (‘coow’) = (4 + 1 + 1 + 3) * 4 = 36。
给定团体和总座位数,任务是最大化幸福感并打印旅行团体的最大幸福感。
例子:

处理方法:该问题可以认为是对 0-1 背包问题的轻微修改。可用的总座位数可视为背包的大小。每个组的幸福度可以看作是每个项目的利润,每个组的人数可以看作是每个项目的权重。现在类似于0-1背包问题的动态规划方法在这里应用动态规划以获得最大的幸福感。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximized happiness
int MaxHappiness(int A, int N, vector v)
{
    string str;
 
    // Two arrays similar to
    // 0 1 knapsack problem
    int val[N], wt[N], c = 0;
    for (int i = 0; i < N; i++) {
        str = v[i];
 
        // To store the happiness
        // of the current group
        c = 0;
        for (int j = 0; str[j]; j++) {
 
            // Current person is a child
            if (str[j] == 'c')
                c += 4;
 
            // Woman
            else if (str[j] == 'w')
                c += 3;
 
            // Man
            else if (str[j] == 'm')
                c += 2;
 
            // Old person
            else
                c++;
        }
 
        // Group's happiness is the sum of happiness
        // of the people in the group multiplied by
        // the number of people
        c *= str.length();
        val[i] = c;
        wt[i] = str.length();
    }
 
    // Solution using 0 1 knapsack
    int k[N + 1][A + 1];
    for (int i = 0; i <= N; i++) {
        for (int w = 0; w <= A; w++) {
            if (i == 0 || w == 0)
                k[i][w] = 0;
            else if (wt[i - 1] <= w)
                k[i][w] = max(val[i - 1]
                                  + k[i - 1][w - wt[i - 1]],
                              k[i - 1][w]);
            else
                k[i][w] = k[i - 1][w];
        }
    }
    return k[N][A];
}
 
// Driver code
int main()
{
 
    // Number of seats
    int A = 5;
 
    // Groups
    vector v = { "mmo", "oo", "cmw", "cc", "c" };
    int N = v.size();
    cout << MaxHappiness(A, N, v);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
    // Function to return the maximized happiness
    static int maxHappiness(int A, int N, String[] v)
    {
        String str;
 
        // Two arrays similar to
        // 0 1 knapsack prolem
        int[] val = new int[N];
        int[] wt = new int[N];
        int c = 0;
        for (int i = 0; i < N; i++)
        {
            str = v[i];
 
            // To store the happiness
            // of the current goup
            c = 0;
            for (int j = 0; j < str.length(); j++)
            {
                // Current person is a child
                if (str.charAt(j) == 'c')
                    c += 4;
 
                // Woman
                else if (str.charAt(j) == 'w')
                    c += 3;
 
                // Man
                else if (str.charAt(j) == 'm')
                    c += 2;
 
                // Old Person
                else
                    c++;
            }
 
            // Group's happiness is the sum of happiness
            // of the people in the group multiplie
            // the number of people
            c *= str.length();
            val[i] = c;
            wt[i] = str.length();
        }
 
        // Solution using 0 1 knapsack
        int[][] k = new int[N + 1][A + 1];
        for (int i = 0; i <= N; i++)
        {
            for (int w = 0; w <= A; w++)
            {
                if (i == 0 || w == 0)
                    k[i][w] = 0;
                else if (wt[i - 1] <= w)
                {
                    k[i][w] = Math.max(val[i - 1]+ k[i - 1][w - wt[i - 1]], k[i-1][w]);
                }
                else
                {
                    k[i][w] = k[i - 1][w];
                }
            }
        }
        return k[N][A];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Number of seats
        int A = 5;
 
        // Groups
        String[] v = { "mmo", "oo", "cmw", "cc", "c" };
        int N = v.length;
        System.out.println(maxHappiness(A, N, v));
    }
}
 
// This code is contributed by Vivek Kumar Singh


Python3
# Python3 implementation of the approach
import numpy as np
 
# Function to return the maximized happiness
def MaxHappiness(A, N, v) :
     
    # Two arrays similar to
    # 0 1 knapsack problem
    val = [0] * N; wt = [0] * N; c = 0;
     
    for i in range(N) :
        string = v[i];
 
        # To store the happiness
        # of the current group
        c = 0;
        for j in range(len(string)) :
 
            # Current person is a child
            if (string[j] == 'c') :
                c += 4;
 
            # Woman
            elif (string[j] == 'w') :
                c += 3;
 
            # Man
            elif (string[j] == 'm') :
                c += 2;
 
            # Old person
            else :
                c += 1;
 
        # Group's happiness is the sum of happiness
        # of the people in the group multiplied by
        # the number of people
        c *= len(string);
         
        val[i] = c;
         
        wt[i] = len(string);
 
    # Solution using 0 1 knapsack
    k = np.zeros((N + 1, A + 1))
     
    for i in range(N + 1) :
         
        for w in range(A + 1) :
            if (i == 0 or w == 0) :
                k[i][w] = 0;
            elif (wt[i - 1] <= w) :
                k[i][w] = max(val[i - 1] +
                                k[i - 1][w - wt[i - 1]],
                                k[i - 1][w]);
            else :
                k[i][w] = k[i - 1][w];
                 
    return k[N][A];
 
# Driver code
if __name__ == "__main__" :
 
    # Number of seats
    A = 5;
 
    # Groups
    v = [ "mmo", "oo", "cmw", "cc", "c" ];
     
    N = len(v);
    print(MaxHappiness(A, N, v));
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
    // Function to return the maximized happiness
    static int maxHappiness(int A, int N,
                              String[] v)
    {
        String str;
 
        // Two arrays similar to
        // 0 1 knapsack prolem
        int[] val = new int[N];
        int[] wt = new int[N];
        int c = 0;
        for (int i = 0; i < N; i++)
        {
            str = v[i];
 
            // To store the happiness
            // of the current goup
            c = 0;
            for (int j = 0; j < str.Length; j++)
            {
                // Current person is a child
                if (str[j] == 'c')
                    c += 4;
 
                // Woman
                else if (str[j] == 'w')
                    c += 3;
 
                // Man
                else if (str[j] == 'm')
                    c += 2;
 
                // Old Person
                else
                    c++;
            }
 
            // Group's happiness is the sum of happiness
            // of the people in the group multiplie
            // the number of people
            c *= str.Length;
            val[i] = c;
            wt[i] = str.Length;
        }
 
        // Solution using 0 1 knapsack
        int[ , ] k = new int[N + 1, A + 1];
        for (int i = 0; i <= N; i++)
        {
            for (int w = 0; w <= A; w++)
            {
                if (i == 0 || w == 0)
                    k[i, w] = 0;
                else if (wt[i - 1] <= w)
                {
                    k[i, w] = Math.Max(val[i - 1]+
                                       k[i - 1, w - wt[i - 1]],
                                       k[i - 1, w]);
                }
                else
                {
                    k[i, w] = k[i - 1, w];
                }
            }
        }
        return k[N, A];
    }
 
    // Driver code
    public static void Main()
    {
        // Number of seats
        int A = 5;
 
        // Groups
        String[] v = { "mmo", "oo", "cmw", "cc", "c" };
        int N = v.Length;
        Console.WriteLine(maxHappiness(A, N, v));
    }
}
 
// This code is contributed by Mohit kumar 29


Javascript


输出:
43