通过对未排序的子数组进行排序来最小化对给定数组进行排序的成本
给定一个大小为N的数组arr[] ,任务是通过对任何未排序的子数组进行排序来最小化排序数组的成本,其中操作的成本是该子数组的最大和最小元素之间的差。此操作可以无限次执行,包括 0。
例子:
Input: arr[] = {1, 7, 5, 2, 1, 8}
Output: 6
Explanation: The subarray from index [1,4] can be chosen and can be sorted with cost = 7 – 1 = 6
Input: arr[] = { 1, 4, 3, 5, 6 ,13, 10}
Output: 4
Explanation: The subarray from index index [1,2] and [5,6] can be sorted with cost of 4 – 3 and 13 – 10 = 1 + 3 = 4
方法:这可以使用贪心方法来解决,已经排序的元素不需要任何成本,因此只有具有未排序元素的子数组必须考虑排序。 Multiset 可用于存储未排序的元素,并且 multiset 的最后一个元素和第一个元素之间的差异给出了成本。
按照以下步骤解决上述问题:
- 初始化一个向量v并将arr复制到其中并将向量的大小分配给变量 n。
- 现在对向量v进行排序。
- 初始化 2 个多重集m1和m2分别存储未排序和排序的子数组, cost = 0存储结果。
- 现在遍历范围[0,N)并检查
- 如果v[i]不等于arr[i]
- 将arr[i]插入m1并将v[i]插入m2
- 当两个多重集相同时,将集合的最后一个元素和第一个元素之间的差异添加到cost中。
- 清除两个多重集以使它们被下一个未排序的子数组使用。
- 打印成本。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost
// to sort the array in ascending order
int minimum_cost(vector arr)
{
// Copy the arr into vector and sort it
vector sorted = arr;
int n = arr.size();
sort(sorted.begin(), sorted.end());
// Initialize the two multisets to store
// sorted and the unordered elements
multiset m1, m2;
// Initialize cost to store final answer
int cost = 0;
for (int i = 0; i < n; i++) {
if (sorted[i] != arr[i]) {
m1.insert(arr[i]);
m2.insert(sorted[i]);
// If both the multisets are equal,
// the unordered subarray is sorted
if (m1 == m2) {
// The cost is difference
// between mini and max
cost += (*m1.rbegin() -
*m2.begin());
// Clear the multisets to make
// use of it again
m1.clear();
m2.clear();
}
}
}
return cost;
}
// Driver code
int main()
{
// Initialize the queries
vector arr = { 1, 7, 5, 2, 1, 8 };
cout << minimum_cost(arr);
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
class GFG {
// Function to find the minimum cost
// to sort the array in ascending order
public static int minimum_cost(int[] arr)
{
// Copy the arr into vector and sort it
int n = arr.length;
int[] sorted = new int[n];
sorted = arr.clone();
Arrays.sort(sorted);
// Initialize the two multisets to store
// sorted and the unordered elements
SortedSet m1 = new TreeSet();
SortedSet m2 = new TreeSet();
// Initialize cost to store final answer
int cost = 0;
for (int i = 0; i < n; i++) {
if (sorted[i] != arr[i]) {
m1.add(arr[i]);
m2.add(sorted[i]);
// If both the multisets are equal,
// the unordered subarray is sorted
if (m1.equals(m2))
{
// The cost is difference
// between mini and max
cost += (Collections.max(m1) - Collections.min(m2));
// Clear the multisets to make
// use of it again
m1.clear();
m2.clear();
}
}
}
return cost;
}
// Driver code
public static void main (String[] args)
{
// Initialize the queries
int[] arr = { 1, 7, 5, 2, 1, 8 };
System.out.println(minimum_cost(arr));
}
}
// This code is contributed by Shubham Singh
Python3
# python3 program for the above approach
# Function to find the minimum cost
# to sort the array in ascending order
def minimum_cost(arr):
# Copy the arr into vector and sort it
sorted = arr.copy()
n = len(arr)
sorted.sort()
# Initialize the two multisets to store
# sorted and the unordered elements
m1 = set()
m2 = set()
# Initialize cost to store final answer
cost = 0
for i in range(0, n):
if (sorted[i] != arr[i]):
m1.add(arr[i])
m2.add(sorted[i])
# If both the multisets are equal,
# the unordered subarray is sorted
if (m1 == m2):
# The cost is difference
# between mini and max
cost += (list(m1)[len(list(m1)) - 1] -
list(m2)[0])
# Clear the multisets to make
# use of it again
m1.clear()
m2.clear()
return cost
# Driver code
if __name__ == "__main__":
# Initialize the queries
arr = [1, 7, 5, 2, 1, 8]
print(minimum_cost(arr))
# This code is contributed by rakeshsahni
C#
// C# code to implement the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find the minimum cost
// to sort the array in ascending order
public static int minimum_cost(int[] arr)
{
// Copy the arr into vector and sort it
int n = arr.Length;
int[] sorted = new int[n];
Array.Copy(arr, 0, sorted, 0, n);
Array.Sort(sorted);
// Initialize the two multisets to store
// sorted and the unordered elements
SortedSet m1 = new SortedSet();
SortedSet m2 = new SortedSet();
// Initialize cost to store final answer
int cost = 0;
for (int i = 0; i < n; i++) {
if (sorted[i] != arr[i]) {
m1.Add(arr[i]);
m2.Add(sorted[i]);
// If both the multisets are equal,
// the unordered subarray is sorted
if (m1.SetEquals(m2))
{
// The cost is difference
// between mini and max
cost += (m1.Max - m2.Min);
// Clear the multisets to make
// use of it again
m1.Clear();
m2.Clear();
}
}
}
return cost;
}
// Driver code
public static void Main()
{
// Initialize the queries
int[] arr = { 1, 7, 5, 2, 1, 8 };
Console.Write(minimum_cost(arr));
}
}
// This code is contributed by Shubham Singh
Javascript
输出
6
时间复杂度: O(N * logN)
辅助空间: O(N)