给定两个数组arr []和min [],它们由N个整数和一个整数K组成。对于每个索引i ,arr [i]可以减少到最多min [i] 。考虑一个变量,比如说S (最初为0 )。任务是找到可以通过执行以下操作获得的S的最大值:
- 选择一个索引i并将max(arr [i],min [i])添加到S。
- 从arr []中删除所选元素及其对应的最小值。
- 如果每个剩余值大于其在min []中对应的最小值,则将其减少K。
例子:
Input: arr[] = {2, 4, 5, 8}, min[] = {1, 4, 5, 5}, K = 3
Output: 18
Explanation:
Choose the elements in the following order:
Step 1: Choose element 8. Now, S = 8 and arr[] = {-1, 4, 5} and min[] = {1, 4, 5}
Step 2: Choose element 5. Now, S = 8 + 5 = 13 and arr[] = {-1, 4} and min[] = {1, 4}
Step 3: Choose element 5. Now, S = 8 + 5 + 4 = 17 and arr[] = {-1} and min[] = {1}
Step 4: Choose element -1, but -1 < min[0]. Therefore, choose 1.
Hence, S = 8 + 5 + 4 + 1 = 18.
Input: arr[] = {3, 5, 2, 1}, min[] = {3, 2, 1, 3}, K = 2
Output: 12
Explanation:
Choose the elements in the following order:
Step 1: Choose element 5. Now, S = 5 and arr[] = {3, 0, 1} and min[] = {3, 1, 3}
Step 2: Choose element 3. Now, S = 5 + 3 = 8 and arr[] = {0, 1} and min[] = {1, 3}
Step 3: Choose element 3 from min[]. Now, S = 5 + 3 + 3 = 11 and arr[] = {0} and min[] = {1}
Step 4: Choose element 1 from min[].
Hence, S = 5 + 3 + 3 + 1 = 12.
天真的方法:最简单的方法是遍历给定的数组并在数组本身中执行给定的操作。请按照以下步骤解决问题:
- 遍历数组并找到最大数组元素。
- 在S中添加最大值,然后删除最大值。
- 如果满足上述条件,则将剩余元素减少K。
- 重复上述步骤,直到给定数组为空。
- 遍历后,打印S。
时间复杂度: O(N 2 ),其中N是给定数组的长度。
辅助空间: O(N)
高效方法:想法是按降序对给定数组进行排序。然后,通过贪婪地选择元素来打印S的最大值。请按照以下步骤解决问题:
- 将数组arr []的元素与它们在min []中的对应值配对。
- 根据数组arr []以降序对对数组进行排序。
- 最初选择最大元素,然后将K增加其初始值。
- 现在,通过将其值减去当前K来选择下一个最大元素。
- 重复上述步骤,直到遍历所有数组元素并输出S的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// the array arr[] where each element
// can be reduced to at most min[i]
void findMaxSum(vector arr, int n,
vector min, int k,
int& S)
{
// Stores the pair of arr[i] & min[i]
vector > A;
for (int i = 0; i < n; i++) {
A.push_back({ arr[i], min[i] });
}
// Sorting vector of pairs
sort(A.begin(), A.end(),
greater >());
int K = 0;
// Traverse the vector of pairs
for (int i = 0; i < n; i++) {
// Add to the value of S
S += max(A[i].first - K,
A[i].second);
// Update K
K += k;
}
}
// Driver Code
int main()
{
vector arr, min;
// Given array arr[], min[]
arr = { 3, 5, 2, 1 };
min = { 3, 2, 1, 3 };
int N = arr.size();
// Given K
int K = 3;
int S = 0;
// Function Call
findMaxSum(arr, N, min, K, S);
// Print the value of S
cout << S;
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
static int S;
// Function to find the maximum sum of
// the array arr[] where each element
// can be reduced to at most min[i]
static void findMaxSum(int[] arr, int n,
int[] min, int k)
{
// Stores the pair of arr[i] & min[i]
ArrayList A = new ArrayList<>();
for(int i = 0; i < n; i++)
{
A.add(new int[]{arr[i], min[i]});
}
// Sorting vector of pairs
Collections.sort(A, (a, b) -> b[0] - a[0]);
int K = 0;
// Traverse the vector of pairs
for(int i = 0; i < n; i++)
{
// Add to the value of S
S += Math.max(A.get(i)[0] - K,
A.get(i)[1]);
// Update K
K += k;
}
}
// Driver code
public static void main (String[] args)
{
// Given array arr[], min[]
int[] arr = { 3, 5, 2, 1 };
int[] min = { 3, 2, 1, 3 };
int N = arr.length;
// Given K
int K = 3;
S = 0;
// Function Call
findMaxSum(arr, N, min, K);
// Print the value of S
System.out.println(S);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to find the maximum sum of
# the array arr[] where each element
# can be reduced to at most min[i]
def findMaxSum(arr, n, min, k, S):
# Stores the pair of arr[i] & min[i]
A = []
for i in range(n):
A.append((arr[i], min[i]))
# Sorting vector of pairs
A = sorted(A)
A = A[::-1]
K = 0
# Traverse the vector of pairs
for i in range(n):
# Add to the value of S
S += max(A[i][0] - K, A[i][1])
# Update K
K += k
return S
# Driver Code
if __name__ == '__main__':
arr, min = [], []
# Given array arr[], min[]
arr = [ 3, 5, 2, 1 ]
min = [ 3, 2, 1, 3 ]
N = len(arr)
# Given K
K = 3
S = 0
# Function Call
S = findMaxSum(arr, N, min, K, S)
# Print the value of S
print(S)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
static int S;
// Function to find the maximum sum of
// the array arr[] where each element
// can be reduced to at most min[i]
static void findMaxSum(int[] arr, int n,
int[] min, int k)
{
// Stores the pair of arr[i] & min[i]
List> A = new List>();
for(int i = 0; i < n; i++)
{
A.Add(new List());
A[i].Add(arr[i]);
A[i].Add(min[i]);
}
// Sorting vector of pairs
A = A.OrderBy(lst => lst[0]).ToList();
A.Reverse();
int K = 0;
// Traverse the vector of pairs
for(int i = 0; i < n; i++)
{
// Add to the value of S
S += Math.Max(A[i][0] - K, A[i][1]);
// Update K
K += k;
}
}
// Driver code
static public void Main()
{
// Given array arr[], min[]
int[] arr = { 3, 5, 2, 1 };
int[] min = { 3, 2, 1, 3 };
int N = arr.Length;
// Given K
int K = 3;
S = 0;
// Function Call
findMaxSum(arr, N, min, K);
// Print the value of S
Console.WriteLine(S);
}
}
// This code is contributed by avanitrachhadiya2155
12
时间复杂度: O(N * log N),其中N是给定数组的长度。
辅助空间: O(N)