📌  相关文章
📜  满足给定条件所需的最小增量/减量运算

📅  最后修改于: 2021-04-29 11:59:43             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到在任何索引i处所需的最小递增或递减操作数,以便对于每个i (1≤i 如果索引处元素的总和从1i,则从1i + 1的元素之和必须为,反之亦然。
注意:将数组视为基于1的索引。

例子:

方法:如果每个i从1到N – 1,则数组将满足条件:

  • 如果i为奇数,则从1到i的元素之和为
  • 如果我是偶数 从1到i的元素之和为,反之亦然。

尝试上述两种可能性,然后选择操作次数最少的一种。步骤如下:

  1. 初始化变量num_of_ops = 0,表示到目前为止已完成的操作数。
  2. 对于任何索引i ,如果i偶数且元素的总和为1到i 负数,然后在arr [i]中加上(1+ | sum |)使其为正数。现在从1到i的元素之和为1 。还要在num_of_ops中添加(1+ | sum |) ,以计算操作数。
  3. 如果i奇数,并且从1到i的元素之和为,则从a [i]减去(1+ | sum |)使其为。现在从1到i的元素之和为-1。还要在num_of_ops中添加(1+ | sum |) 。即,计算操作次数。
  4. 类似地,求出偶数i,直到i为负的元素之和,以及奇数i直到i为正的元素之和的运算次数。
  5. 从以上两个过程中选择最少的操作数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find minimum number
// of operations to get desired array
int minOperations(int a[], int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    // For even 'i', sum of
    // elements till 'i' is negative
  
    // For odd 'i', sum of
    // elements till 'i' is positive
    for (int i = 0; i < N; i++) {
        sum += a[i];
  
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0) {
            num_of_ops1 += (1 + abs(sum));
            sum = -1;
        }
  
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0) {
            num_of_ops1 += (1 + abs(sum));
            sum = 1;
        }
    }
  
    sum = 0;
  
    // For even 'i', the sum of
    // elements till 'i' is positive
  
    // For odd 'i', sum of
    // elements till 'i' is negative
    for (int i = 0; i < N; i++) {
        sum += a[i];
  
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting  1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0) {
            num_of_ops2 += (1 + abs(sum));
            sum = -1;
        }
  
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0) {
            num_of_ops2 += (1 + abs(sum));
            sum = 1;
        }
    }
  
    // Return the minimum of the two
    return min(num_of_ops1, num_of_ops2);
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 3, -4, 5, 0, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << minOperations(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to find minimum number
// of operations to get desired array
static int minOperations(int a[], int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    // For even 'i', sum of
    // elements till 'i' is negative
  
    // For odd 'i', sum of
    // elements till 'i' is positive
    for (int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0) 
        {
            num_of_ops1 += (1 + Math.abs(sum));
            sum = -1;
        }
  
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0) 
        {
            num_of_ops1 += (1 + Math.abs(sum));
            sum = 1;
        }
    }
  
    sum = 0;
  
    // For even 'i', the sum of
    // elements till 'i' is positive
  
    // For odd 'i', sum of
    // elements till 'i' is negative
    for (int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting  1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0) 
        {
            num_of_ops2 += (1 + Math.abs(sum));
            sum = -1;
        }
  
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0)
        {
            num_of_ops2 += (1 + Math.abs(sum));
            sum = 1;
        }
    }
  
    // Return the minimum of the two
    return Math.min(num_of_ops1, num_of_ops2);
}
  
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int arr[] = { 3, -4, 5, 0, 1 };
    int N = arr.length;
  
    // Function Call
    System.out.print(minOperations(arr, N));
}
}
  
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
  
# Function to find minimum number
# of operations to get desired array
def minOperations(a, N):
  
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    # For even 'i', sum of
    # elements till 'i' is negative
  
    # For odd 'i', sum of
    # elements till 'i' is positive
    for i in range(N):
        sum += a[i]
  
        # If i is even and sum is positive,
        # make it negative by subtracting
        # 1 + |s| from a[i]
        if (i % 2 == 0 and sum >= 0):
            num_of_ops1 += (1 + abs(sum))
            sum = -1
  
        # If i is odd and sum is negative,
        # make it positive by
        # adding 1 + |s| into a[i]
        elif (i % 2 == 1 and sum <= 0):
            num_of_ops1 += (1 + abs(sum))
            sum = 1
  
    sum = 0
  
    # For even 'i', the sum of
    # elements till 'i' is positive
  
    # For odd 'i', sum of
    # elements till 'i' is negative
    for i in range (N):
        sum += a[i]
  
        # Check if 'i' is odd and sum is
        # positive, make it negative by
        # subtracting 1 + |s| from a[i]
        if (i % 2 == 1 and sum >= 0):
            num_of_ops2 += (1 + abs(sum))
            sum = -1
  
        # Check if 'i' is even and sum
        # is negative, make it positive
        # by adding 1 + |s| into a[i]
        elif (i % 2 == 0 and sum <= 0):
            num_of_ops2 += (1 + abs(sum))
            sum = 1
  
    # Return the minimum of the two
    return min(num_of_ops1, num_of_ops2)
  
# Driver Code
if __name__ == "__main__":
      
    # Given array arr[]
    arr = [ 3, -4, 5, 0, 1 ]
    N = len(arr)
  
    # Function call
    print(minOperations(arr, N))
  
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to find minimum number
// of operations to get desired array
static int minOperations(int []a, int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    // For even 'i', sum of
    // elements till 'i' is negative
  
    // For odd 'i', sum of
    // elements till 'i' is positive
    for(int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0) 
        {
            num_of_ops1 += (1 + Math.Abs(sum));
            sum = -1;
        }
  
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0) 
        {
            num_of_ops1 += (1 + Math.Abs(sum));
            sum = 1;
        }
    }
  
    sum = 0;
  
    // For even 'i', the sum of
    // elements till 'i' is positive
  
    // For odd 'i', sum of
    // elements till 'i' is negative
    for(int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting 1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0) 
        {
            num_of_ops2 += (1 + Math.Abs(sum));
            sum = -1;
        }
  
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0)
        {
            num_of_ops2 += (1 + Math.Abs(sum));
            sum = 1;
        }
    }
  
    // Return the minimum of the two
    return Math.Min(num_of_ops1, num_of_ops2);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given array []arr
    int []arr = { 3, -4, 5, 0, 1 };
    int N = arr.Length;
  
    // Function call
    Console.Write(minOperations(arr, N));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
6

时间复杂度: O(N)
辅助空间: O(1)