最大化通过将给定数组划分为给定大小而获得的每个 K 数组的最大值和最小值的总和
给定两个数组,大小为N的arr[]和大小为K的div[] 。将arr[]分成K个不同的数组,每个div[i]的大小。任务是在最大化每个划分数组的最大值和最小值之和后找到总和。
例子:
Input: arr[] = {3, 1, 7, 4}, div[] = {1, 3}, N = 4, K = 2
Output: 19
Explanation: Divide the array in the following way:
- {7}, sum of maximum and minimum = (7 + 7) = 14
- {1, 3, 4}, sum of maximum and minimum = (4 + 1) = 5
Total sum = 14 + 5 = 19.
Input: arr[] = {10, 12, 10, 12, 10, 12}, div[] = {3, 3}, N = 6, K = 2
Output: 44
方法:按照以下步骤解决问题:
- 取一个变量说count1来计算div[]中的1的数量。
- 对两个数组进行排序, arr[]以降序排列, div[]以升序排列。
- 取一个变量说, ans来存储答案,另一个变量说t ,它表示从div[]中的哪个索引迭代开始。
- 迭代数组直到K ,在每次迭代中将元素添加到 ans 中,然后在count1大于0时再次将该元素添加到ans中,因为数组的大小为 1 ,同时具有与maximum和minimum相同的元素。
- 再次从第K个索引迭代一个循环到数组的末尾。向ans添加一个元素并更新索引。
- 返回ans 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the total sum after
// maximizing the sum of maximum and
// minimum of each divided array
int maximizeSum(int arr[], int divi[], int N, int K)
{
// Variable to count 1s in divi[]
int count1 = 0;
for (int i = 0; i < K; i++) {
if (divi[i] == 1) {
count1++;
}
}
// Sort arr[] in descending order
sort(arr, arr + N, greater());
// Sort divi[] in ascending order
sort(divi, divi + K);
// Temporary variable to store
// the count of 1s in the divi[]
int t = count1;
// Variable to store answer
int ans = 0;
// Iterate over the array till K
for (int i = 0; i < K; i++) {
// Add the current element to ans
ans += arr[i];
// If count1 is greater than 0,
// decrement it by 1 and update the
// ans by again adding the same element
if (count1 > 0) {
count1--;
ans += arr[i];
}
}
// Traverse the array from Kth index
// to the end
for (int i = K; i < N; i++) {
// Update the index
i += divi[t] - 2;
// Add the value at that index to ans
ans += arr[i];
t++;
}
// Return ans
return ans;
}
// Driver Code
int main()
{
int arr[] = { 3, 1, 7, 4 };
int divi[] = { 1, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = sizeof(divi) / sizeof(divi[0]);
cout << maximizeSum(arr, divi, N, K);
return 0;
}
Java
// Java code for the above approach
import java.util.Arrays;
import java.util.Collections;
class GFG
{
// Function to find the total sum after
// maximizing the sum of maximum and
// minimum of each divided array
static int maximizeSum(int arr[], int divi[], int N,
int K)
{
// Variable to count 1s in divi[]
int count1 = 0;
for (int i = 0; i < K; i++) {
if (divi[i] == 1) {
count1++;
}
}
// Sort arr[] in descending order
Arrays.sort(arr);
reverse(arr);
// Sort divi[] in ascending order
Arrays.sort(divi);
// Temporary variable to store
// the count of 1s in the divi[]
int t = count1;
// Variable to store answer
int ans = 0;
// Iterate over the array till K
for (int i = 0; i < K; i++) {
// Add the current element to ans
ans += arr[i];
// If count1 is greater than 0,
// decrement it by 1 and update the
// ans by again adding the same element
if (count1 > 0) {
count1--;
ans += arr[i];
}
}
// Traverse the array from Kth index
// to the end
for (int i = K; i < N; i++) {
// Update the index
i += divi[t] - 2;
// Add the value at that index to ans
ans += arr[i];
t++;
}
// Return ans
return ans;
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 1, 7, 4 };
int divi[] = { 1, 3 };
int N = arr.length;
int K = divi.length;
System.out.println(maximizeSum(arr, divi, N, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function to find the total sum after
# maximizing the sum of maximum and
# minimum of each divided array
def maximizeSum(arr, divi, N, K):
# Variable to count 1s in divi[]
count1 = 0
for i in range(K):
if (divi[i] == 1):
count1 += 1
# Sort arr[] in descending order
arr.sort()
arr.reverse()
# Sort divi[] in ascending order
divi.sort()
# Temporary variable to store
# the count of 1s in the divi[]
t = count1
# Variable to store answer
ans = 0
# Iterate over the array till K
for i in range(K):
# Add the current element to ans
ans += arr[i]
# If count1 is greater than 0,
# decrement it by 1 and update the
# ans by again adding the same element
if (count1 > 0):
count1 -= 1
ans += arr[i]
# Traverse the array from Kth index
# to the end
i = K
while(i < N):
# Update the index
i += divi[t] - 2
# Add the value at that index to ans
ans += arr[i]
t += 1
i += 1
# Return ans
return ans
# Driver Code
arr = [3, 1, 7, 4]
divi = [1, 3]
N = len(arr)
K = len(divi)
print(maximizeSum(arr, divi, N, K))
# This code is contributed by gfgking
C#
// C# code for the above approach
using System;
public class GFG
{
// Function to find the total sum after
// maximizing the sum of maximum and
// minimum of each divided array
static int maximizeSum(int []arr, int []divi, int N,
int K)
{
// Variable to count 1s in divi[]
int count1 = 0;
for (int i = 0; i < K; i++) {
if (divi[i] == 1) {
count1++;
}
}
// Sort arr[] in descending order
Array.Sort(arr);
reverse(arr);
// Sort divi[] in ascending order
Array.Sort(divi);
// Temporary variable to store
// the count of 1s in the divi[]
int t = count1;
// Variable to store answer
int ans = 0;
// Iterate over the array till K
for (int i = 0; i < K; i++) {
// Add the current element to ans
ans += arr[i];
// If count1 is greater than 0,
// decrement it by 1 and update the
// ans by again adding the same element
if (count1 > 0) {
count1--;
ans += arr[i];
}
}
// Traverse the array from Kth index
// to the end
for (int i = K; i < N; i++) {
// Update the index
i += divi[t] - 2;
// Add the value at that index to ans
ans += arr[i];
t++;
}
// Return ans
return ans;
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.Length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { 3, 1, 7, 4 };
int []divi = { 1, 3 };
int N = arr.Length;
int K = divi.Length;
Console.WriteLine(maximizeSum(arr, divi, N, K));
}
}
// This code is contributed by AnkThon
Javascript
输出
19
时间复杂度: O(N)
辅助空间: O(1)