📌  相关文章
📜  最大和递减子序列

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

给定一个由 N 个正整数组成的数组。任务是找到给定数组的最大和递减子序列(MSDS)的总和,使得子序列中的整数按降序排序。
例子

此问题是最长递减子序列问题的变体。上述问题的最优子结构将是:
设 arr[0..n-1] 是输入数组, MSDS[i] 是在索引 i 处结束的 MSDS 的最大总和,这样 arr[i] 是 MSDS 的最后一个元素。
那么,MSDS[i] 可以写成:

要找到给定数组的 MSDS,我们需要返回 max(MSDS[i]) 其中 n > i > 0。
下面是上述方法的实现:

C++
// CPP code to return the maximum sum
// of decreasing subsequence in arr[]
#include 
using namespace std;
 
// function to return the maximum
// sum of decreasing subsequence
// in arr[]
int maxSumDS(int arr[], int n)
{
    int i, j, max = 0;
    int MSDS[n];
 
    // Initialize msds values
    // for all indexes
    for (i = 0; i < n; i++)
        MSDS[i] = arr[i];
 
    // Compute maximum sum values
    // in bottom up manner
    for (i = 1; i < n; i++)
        for (j = 0; j < i; j++)
            if (arr[i] < arr[j] && MSDS[i] < MSDS[j] + arr[i])
                MSDS[i] = MSDS[j] + arr[i];
 
    // Pick maximum of all msds values
    for (i = 0; i < n; i++)
        if (max < MSDS[i])
            max = MSDS[i];
 
    return max;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 4, 100, 3, 2, 101, 1 };
     
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Sum of maximum sum decreasing subsequence is: "
         << maxSumDS(arr, n);
    return 0;
}


Java
// Java code to return the maximum sum
// of decreasing subsequence in arr[]
import java.io.*;
import java.lang.*;
 
class GfG {
     
    // function to return the maximum
    // sum of decreasing subsequence
    // in arr[]
    public static int maxSumDS(int arr[], int n)
    {
        int i, j, max = 0;
        int[] MSDS = new int[n];
     
        // Initialize msds values
        // for all indexes
        for (i = 0; i < n; i++)
            MSDS[i] = arr[i];
     
        // Compute maximum sum values
        // in bottom up manner
        for (i = 1; i < n; i++)
            for (j = 0; j < i; j++)
                if (arr[i] < arr[j] &&
                    MSDS[i] < MSDS[j] + arr[i])
                    MSDS[i] = MSDS[j] + arr[i];
     
        // Pick maximum of all msds values
        for (i = 0; i < n; i++)
            if (max < MSDS[i])
                max = MSDS[i];
     
        return max;
    }
     
    // Driver Code
    public static void main(String argc[])
    {
        int arr[] = { 5, 4, 100, 3, 2, 101, 1 };
         
        int n = 7;
     
        System.out.println("Sum of maximum sum"
               + " decreasing subsequence is: "
                           + maxSumDS(arr, n));
    }
}
 
// This code os contributed by Sagar Shukla.


Python3
# Python3 code to return the maximum sum
# of decreasing subsequence in arr[]
 
# Function to return the maximum
# sum of decreasing subsequence
# in arr[]
def maxSumDS(arr, n):
     
    i, j, max = (0, 0, 0)
     
    MSDS=[0 for i in range(n)]
  
    # Initialize msds values
    # for all indexes
    for i in range(n):
        MSDS[i] = arr[i]
  
    # Compute maximum sum values
    # in bottom up manner
    for i in range(1, n):
        for j in range(i):
            if (arr[i] < arr[j] and
                MSDS[i] < MSDS[j] + arr[i]):
                MSDS[i] = MSDS[j] + arr[i]
  
    # Pick maximum of all msds values
    for i in range(n):
        if (max < MSDS[i]):
            max = MSDS[i]
 
    return max
     
if __name__ == "__main__":
     
    arr=[5, 4, 100, 3,
         2, 101, 1]
    n=len(arr)
    print("Sum of maximum sum decreasing subsequence is: ",
           maxSumDS(arr, n))
 
# This code is contributed by Rutvik_56


C#
// C# code to return the
// maximum sum of decreasing
// subsequence in arr[]
using System;
 
class GFG
{
     
    // function to return the
    // maximum sum of decreasing
    // subsequence in arr[]
    public static int maxSumDS(int []arr,
                               int n)
    {
        int i, j, max = 0;
        int[] MSDS = new int[n];
     
        // Initialize msds values
        // for all indexes
        for (i = 0; i < n; i++)
            MSDS[i] = arr[i];
     
        // Compute maximum sum values
        // in bottom up manner
        for (i = 1; i < n; i++)
            for (j = 0; j < i; j++)
                if (arr[i] < arr[j] &&
                    MSDS[i] < MSDS[j] + arr[i])
                    MSDS[i] = MSDS[j] + arr[i];
     
        // Pick maximum of
        // all msds values
        for (i = 0; i < n; i++)
            if (max < MSDS[i])
                max = MSDS[i];
     
        return max;
    }
     
    // Driver Code
    static public void Main ()
    {
        int []arr = {5, 4, 100,
                     3, 2, 101, 1};
        int n = 7;
        Console.WriteLine("Sum of maximum sum" +
                " decreasing subsequence is: " +
                              maxSumDS(arr, n));
    }
}
 
// This code is contributed by m_kit


PHP


Javascript


输出

Sum of maximum sum decreasing subsequence is: 106

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程