字节之城ByteLand将组织一次神秘之地之旅。不幸的是,说A的席位有限,有N个团体。每个组可以有老人o ,孩子c ,男人m和女人w 。组委会希望最大限度地提高旅途的幸福感。旅行的幸福价值是所有参加活动的人群的幸福价值的总和。如果每个成员都可以坐下,一个小组将去旅行(打破一个小组不是一件好事)。
- 孩子的幸福感c = 4
- 女人的幸福w = 3
- 人的幸福m = 2
- 老人的幸福度o = 1
G组的幸福,H(G)=(其中的人们的幸福总和)*(组中的人数)。
群体的幸福感(’coow’)=(4 +1 +1 + 3)* 4 = 36。
在给定组和总座位数的情况下,任务是最大化幸福感并打印出旅途中各组的最大幸福感。
例子:
Input: groups[] = {“mmo”, “oo”, “cmw”, “cc”, “c”}, A = 5
Output: 43
Pick these groups [‘cmw’, ‘cc’] to get the maximum profit of (4 + 2 + 3) * 3 + (4 + 4) * 2 = 43
Input: groups[] = {“ccc”, “oo”, “cm”, “mm”, “wwo”}, A = 10
Output: 77
方法:该问题可视为对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
43
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。