给定一个由N个整数组成的数组arr [] ,可以一次对任意一个元素执行以下三个操作:
- 将一个添加到元素。
- 从元素中减去一个。
- 保持元素不变。
任务是找到将其转换为几何级数所需的最低成本,并找到公比。
注意:每次加减法费用为1个单位。
例子:
Input: N = 6, arr[] = {1, 11, 4, 27, 15, 33}
Output: 28 2
Explanation:
For r = 1, arr[] = {1, 4, 11, 15, 27, 33}
expected[] = {1, 1, 1, 1, 1, 1}
cost[] = {0, 3, 10, 14, 26, 32}
Total cost: ∑ cost = 85
For r = 2, arr[] = {1, 4, 11, 15, 27, 33}
expected[] = {1, 2, 4, 8, 16, 32}
cost[] = {0, 2, 7, 7, 11, 1}
Total cost: ∑ cost = 28
For r = 3, arr[] = {1, 4, 11, 15, 27, 33}
expected[] = {1, 3, 9, 27, 81, 243}
cost[] = {0, 1, 2, 12, 54, 210}
Total cost: ∑ cost = 279
Minimum cost = 28
Common ratio = 2
Input: N = 7, arr[] = {1, 2, 4, 8, 9, 6, 7}
Output: 30 1
方法:想法是在可能的通用比率范围内进行迭代,并检查所需的最小操作数。请按照以下步骤解决问题:
- 对数组进行排序。
- 使用公式Ceil(arr [maximum_element] /(N – 1))查找可能的公共比率范围。
- 计算所有可能的通用比率所需的操作次数。
- 查找任何常见比率所需的最小操作。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find minimum cost
void minCost(int arr[], int n)
{
if (n == 1) {
cout << 0 << endl;
return;
}
// Sort the array
sort(arr, arr + n);
// Maximum possible common ratios
float raised = 1 / float(n - 1);
float temp = pow(arr[n - 1], raised);
int r = round(temp) + 1;
int i, j, min_cost = INT_MAX;
int common_ratio = 1;
// Iterate over all possible common ratios
for (j = 1; j <= r; j++) {
int curr_cost = 0, prod = 1;
// Calculate operations required
// for the current common ratio
for (i = 0; i < n; i++) {
curr_cost += abs(arr[i] - prod);
prod *= j;
if (curr_cost >= min_cost)
break;
}
// Calculate minimum cost
if (i == n) {
min_cost = min(min_cost,
curr_cost);
common_ratio = j;
}
}
cout << min_cost << ' ';
cout << common_ratio << ' ';
}
// Driver Code
int main()
{
// Given N
int N = 6;
// Given arr[]
int arr[] = { 1, 11, 4, 27, 15, 33 };
// Function Calling
minCost(arr, N);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to find minimum cost
static void minCost(int arr[],
int n)
{
if (n == 1)
{
System.out.print(0 + "\n");
return;
}
// Sort the array
Arrays.sort(arr);
// Maximum possible common ratios
float raised = 1 / (float)(n - 1);
float temp = (float)Math.pow(arr[n - 1],
raised);
int r = (int)(temp) + 1;
int i, j, min_cost = Integer.MAX_VALUE;
int common_ratio = 1;
// Iterate over all possible
// common ratios
for (j = 1; j <= r; j++)
{
int curr_cost = 0, prod = 1;
// Calculate operations required
// for the current common ratio
for (i = 0; i < n; i++)
{
curr_cost += Math.abs(arr[i] -
prod);
prod *= j;
if (curr_cost >= min_cost)
break;
}
// Calculate minimum cost
if (i == n)
{
min_cost = Math.min(min_cost,
curr_cost);
common_ratio = j;
}
}
System.out.print(min_cost + " ");
System.out.print(common_ratio + " ");
}
// Driver Code
public static void main(String[] args)
{
// Given N
int N = 6;
// Given arr[]
int arr[] = {1, 11, 4,
27, 15, 33};
// Function Calling
minCost(arr, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for above approach
import sys
# Function to find minimum cost
def minCost(arr, n):
if (n == 1):
print(0)
return
# Sort the array
arr = sorted(arr)
# Maximum possible common ratios
raised = 1 / (n - 1)
temp = pow(arr[n - 1], raised)
r = round(temp) + 1
min_cost = sys.maxsize
common_ratio = 1
# Iterate over all possible
# common ratios
for j in range(1, r + 1):
curr_cost = 0
prod = 1
# Calculate operations required
# for the current common ratio
i = 0
while i < n:
curr_cost += abs(arr[i] - prod)
prod *= j
if (curr_cost >= min_cost):
break
i += 1
# Calculate minimum cost
if (i == n):
min_cost = min(min_cost,
curr_cost)
common_ratio = j
print(min_cost, common_ratio)
# Driver Code
if __name__ == '__main__':
# Given N
N = 6
# Given arr[]
arr = [ 1, 11, 4, 27, 15, 33 ]
# Function calling
minCost(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to find minimum cost
static void minCost(int []arr,
int n)
{
if (n == 1)
{
Console.Write(0 + "\n");
return;
}
// Sort the array
Array.Sort(arr);
// Maximum possible common ratios
float raised = 1 / (float)(n - 1);
float temp = (float)Math.Pow(arr[n - 1],
raised);
int r = (int)(temp) + 1;
int i, j, min_cost = int.MaxValue;
int common_ratio = 1;
// Iterate over all possible
// common ratios
for (j = 1; j <= r; j++)
{
int curr_cost = 0, prod = 1;
// Calculate operations required
// for the current common ratio
for (i = 0; i < n; i++)
{
curr_cost += Math.Abs(arr[i] -
prod);
prod *= j;
if (curr_cost >= min_cost)
break;
}
// Calculate minimum cost
if (i == n)
{
min_cost = Math.Min(min_cost,
curr_cost);
common_ratio = j;
}
}
Console.Write(min_cost + " ");
Console.Write(common_ratio + " ");
}
// Driver Code
public static void Main(String[] args)
{
// Given N
int N = 6;
// Given []arr
int []arr = { 1, 11, 4,
27, 15, 33 };
// Function Calling
minCost(arr, N);
}
}
// This code is contributed by gauravrajput1
28 2
时间复杂度: O(N * K),其中K是最大可能的公共比率。
辅助空间: O(1)