根据条件数组重新排列给定数组后,最大化修改值的总和
给定一个二进制数组arr1[]和一个整数数组arr2[] ,每个数组长度为N ,任务是重新排列数组arr2中的元素,以使生成的总成本最大化。生成的总成本是通过对arr2数组中的修改值求和来计算的。这些值以这样一种方式修改,即与arr1数组中的值 0 对应的整数对其他元素没有影响,但与arr1数组中的值 1 对应的整数可以使下一个整数的值加倍。
例子:
Input: N = 2, arr1 = [1, 0], arr2 = [3, 4]
Output: 11
Explanation: Element 3 corresponds to value 1 so it can double the value of the next element. Out of 2 arrangements possible [3, 4] and [4, 3] so in the 1st case cost generated is 3+4*2 = 11 and in the 2nd case the cost generated is 4+3=7
Input: N = 5, arr1 = [1, 0, 1, 0, 1], arr2 = [3, 7, 2, 12, 5]
Output: 53
Explanation: Maximum cost can be generated in the arrangement [3, 7, 2, 5, 12] here 1st, 3rd and 4th elements correspond to value 1 and hence their next elements cost can be doubled so cost is 3+7*2+2+5*2+12*2=53
方法:给定的问题可以通过使用贪心方法来解决。这个想法是按降序对数组进行排序,然后对其进行迭代以计算产生的成本。可以按照以下步骤进行:
- 初始化一个辅助数组arr1并将数组arr2的所有元素复制到其中,在数组arr1中对应的值为 1
- 找到数组arr1 中的最小值,删除 它来自数组并将其存储在一个变量中,比如val
- 对数组arr2进行降序排序
- 初始化一个变量ans来计算产生的最大成本
- 如果数组arr1中的所有元素都是 1,则将除最小值val之外的所有元素的值加倍并返回它们的总和
- else 将arr1元素的值加倍到ans 中,其余所有元素不做修改
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to compute maximum power
int max_pow(vector& arr1, vector& arr2)
{
// Count of 1 in arr1
int cnt = count(arr1.begin(), arr1.end(), 1);
// Keep an array of integers corresponding
// to value 1 in arr1 to eliminate the
// integers contributing to minimum cost
vector cost1;
for (int i = 0; i < arr1.size(); ++i) {
if (arr1[i] == 1)
cost1.push_back(arr2[i]);
}
int val = cost1[0];
for (int i = 1; i < cost1.size(); ++i) {
val = min(val, cost1[i]);
}
// Delete the minimum cost
arr2.erase(find(arr2.begin(), arr2.end(), val));
sort(arr2.rbegin(), arr2.rend());
// Ans for storing max result
int ans = 0;
// Case when all are of type 1
if (arr2.size() == cnt - 1) {
int sum = 0;
for (auto it : arr2) {
sum += it;
}
ans = sum * 2 + val;
}
else {
int sum = 0;
for (auto it : arr2) {
sum += it;
}
for (int i = 0; i < cnt; ++i) {
sum += arr2[i];
}
ans = val + sum;
}
return ans;
}
// Driver code
int main()
{
int N = 5;
vector arr_type = { 1, 0, 1, 0, 1 };
vector arr_power = { 3, 2, 7, 12, 5 };
cout << max_pow(arr_type, arr_power);
return 0;
}
// This code is contributed by rakeshsahni
Python3
# Python implementation for the above approach
# Function to compute maximum power
def max_pow(arr1, arr2):
# Count of 1 in arr1
count = arr1.count(1)
# Keep an array of integers corresponding
# to value 1 in arr1 to eliminate the
# integers contributing to minimum cost
cost1 = []
for i in range(len(arr1)):
if(arr1[i] == 1):
cost1.append(arr2[i])
val = min(cost1)
# Delete the minimum cost
del arr2[arr2.index(val)]
arr2.sort(reverse = True)
# Ans for storing max result
ans = 0
# Case when all are of type 1
if(len(arr2) == count-1):
ans = sum(arr2)*2 + val
else:
ans = val + sum(arr2)+sum(arr2[:count])
return ans
# Driver code
N = 5
arr_type = [1, 0, 1, 0, 1]
arr_power = [3, 2, 7, 12, 5]
print(max_pow(arr_type, arr_power))
Javascript
53
时间复杂度: O(N*log N)
空间复杂度: O(N)