通过移动以成本为值的元素来最小化对数组进行排序的成本
给定一个包含N个正整数的数组arr[] ,任务是通过将数组元素移动到任何位置来找到对给定数组进行排序的最小成本,使得移动该元素的成本是该元素的值。
例子:
Input: arr[] = {7, 1, 2, 3}
Output: 6
Explanation:
Following are the possible set of moves to sort the array with minimum cost:
- Move 1 to the front, arr[] = {1, 7, 2, 3}. cost = 1
- Move 2 to 2nd place, arr[] = {1, 2, 7, 3}. cost = 2
- Move 3 to 3rd place, arr[] = {1, 2, 3, 7}, cost = 3
Therefore, the total cost is (1 + 2 + 3) = 6.
Input: arr[] = {7, 1, 2, 5}
Output: 7
方法:给定的问题可以通过使用动态规划来解决。这个想法是固定形成具有最大和的最长非递减子序列的数组元素,并对所有剩余的数组元素执行给定的移动。请按照以下步骤解决问题:
- 找到具有最大和的最长非递减子序列并将其存储在变量中,例如S 。
- 在上述步骤之后,打印(数组元素的总和- S)的值作为对给定数组排序的结果可能的最小成本。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// non-decreasing subsequence
int maxSumIS(int arr[], int n)
{
int i, j, max = 0;
// Stores the maximum sum of
// subsequence ending at index i
int dp[n];
// Initialize dp[] values for all
// indexes
for (i = 0; i < n; i++)
dp[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]
&& dp[i] < dp[j] + arr[i])
dp[i] = dp[j] + arr[i];
// Pick maximum of all msis values
for (i = 0; i < n; i++) {
if (max < dp[i]) {
max = dp[i];
}
}
// Return the maximum sum as max
return max;
}
// Function to find the minimum cost to
// sort given array in increasing order
int minCostSort(int arr[], int N)
{
// Find the sum of array
int sm = 0;
for (int i = 0; i < N; i++) {
sm += arr[i];
}
// Find the maximum sum non-decreasing
// subsequence
int res = maxSumIS(arr, N);
// Return the minimum cost
return sm - res;
}
// Driver Code
int main()
{
int arr[] = { 7, 1, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << minCostSort(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the maximum sum of
// non-decreasing subsequence
static int maxSumIS(int[] arr, int n)
{
int i, j, max = 0;
// Stores the maximum sum of
// subsequence ending at index i
int[] dp = new int[n];
// Initialize dp[] values for all
// indexes
for (i = 0; i < n; i++)
dp[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]
&& dp[i] < dp[j] + arr[i])
dp[i] = dp[j] + arr[i];
// Pick maximum of all msis values
for (i = 0; i < n; i++) {
if (max < dp[i]) {
max = dp[i];
}
}
// Return the maximum sum as max
return max;
}
// Function to find the minimum cost to
// sort given array in increasing order
static int minCostSort(int[] arr, int N)
{
// Find the sum of array
int sm = 0;
for (int i = 0; i < N; i++) {
sm += arr[i];
}
// Find the maximum sum non-decreasing
// subsequence
int res = maxSumIS(arr, N);
// Return the minimum cost
return sm - res;
}
// Driver Code
public static void main(String []args)
{
int[] arr = { 7, 1, 2, 3 };
int N = arr.length;
System.out.print(minCostSort(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Python3
# python program for the above approach
# Function to find the maximum sum of
# non-decreasing subsequence
def maxSumIS(arr, n):
max = 0
# Stores the maximum sum of
# subsequence ending at index i
dp = [0 for _ in range(n)]
# Initialize dp[] values for all
# indexes
for i in range(0, n):
dp[i] = arr[i]
# Compute maximum sum values
# in bottom up manner
for i in range(1, n):
for j in range(0, i):
if (arr[i] >= arr[j] and dp[i] < dp[j] + arr[i]):
dp[i] = dp[j] + arr[i]
# Pick maximum of all msis values
for i in range(0, n):
if (max < dp[i]):
max = dp[i]
# Return the maximum sum as max
return max
# Function to find the minimum cost to
# sort given array in increasing order
def minCostSort(arr, N):
# Find the sum of array
sm = 0
for i in range(0, N):
sm += arr[i]
# Find the maximum sum non-decreasing
# subsequence
res = maxSumIS(arr, N)
# Return the minimum cost
return sm - res
# Driver Code
if __name__ == "__main__":
arr = [7, 1, 2, 3]
N = len(arr)
print(minCostSort(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the maximum sum of
// non-decreasing subsequence
static int maxSumIS(int[] arr, int n)
{
int i, j, max = 0;
// Stores the maximum sum of
// subsequence ending at index i
int[] dp = new int[n];
// Initialize dp[] values for all
// indexes
for (i = 0; i < n; i++)
dp[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]
&& dp[i] < dp[j] + arr[i])
dp[i] = dp[j] + arr[i];
// Pick maximum of all msis values
for (i = 0; i < n; i++) {
if (max < dp[i]) {
max = dp[i];
}
}
// Return the maximum sum as max
return max;
}
// Function to find the minimum cost to
// sort given array in increasing order
static int minCostSort(int[] arr, int N)
{
// Find the sum of array
int sm = 0;
for (int i = 0; i < N; i++) {
sm += arr[i];
}
// Find the maximum sum non-decreasing
// subsequence
int res = maxSumIS(arr, N);
// Return the minimum cost
return sm - res;
}
// Driver Code
public static void Main()
{
int[] arr = { 7, 1, 2, 3 };
int N = arr.Length;
Console.WriteLine(minCostSort(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
6
时间复杂度: O(N 2 )
辅助空间: O(N)