📜  最小增量操作以使阵列按升序排列

📅  最后修改于: 2021-05-06 17:26:40             🧑  作者: Mango

给定大小为N和X的数组。找到使数组按升序排列所需的最小移动。在每一步中,可以将X添加到数组中的任何元素。

例子

Input : a = { 1, 3, 3, 2 }, X = 2
Output : 3
Explanation : Modified array is { 1, 3, 5, 6 }

Input : a = { 3, 5, 6 }, X = 5
Output : 0

观察

方法

Iterate over the given array and take two numbers when a[i] >= a[i-1] 
and apply above observation.

下面是上述方法的实现:

C++
// CPP program to find minimum moves required
// to make the array in increasing order
#include 
using namespace std;
  
// function to find minimum moves required
// to make the array in increasing order
int MinimumMoves(int a[], int n, int x)
{
    // to store answer
    int ans = 0;
  
    // iterate over an array
    for (int i = 1; i < n; i++) {
  
        // non- increasing order
        if (a[i] <= a[i - 1]) {
            int p = (a[i - 1] - a[i]) / x + 1;
  
            // add moves to answer
            ans += p;
  
            // increase the element
            a[i] += p * x;
        }
    }
  
    // return required answer
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 3, 3, 2 };
    int x = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << MinimumMoves(arr, n, x);
  
    return 0;
}


Java
// Java program to find minimum moves required
// to make the array in increasing order
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG{
// function to find minimum moves required
// to make the array in increasing order
static int MinimumMoves(int a[], int n, int x)
{
    // to store answer
    int ans = 0;
   
    // iterate over an array
    for (int i = 1; i < n; i++) {
   
        // non- increasing order
        if (a[i] <= a[i - 1]) {
            int p = (a[i - 1] - a[i]) / x + 1;
   
            // add moves to answer
            ans += p;
   
            // increase the element
            a[i] += p * x;
        }
    }
   
    // return required answer
    return ans;
}
   
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 3, 3, 2 };
    int x = 2;
    int n = arr.length;
   
    System.out.println(MinimumMoves(arr, n, x));
   
}
}


Python 3
# Python3 program to find minimum 
# moves required to make the array 
# in increasing order 
  
# function to find minimum moves required 
# to make the array in increasing order 
def MinimumMoves(a, n, x) :
  
    # to store answer 
    ans = 0
  
    # iterate over an array
    for i in range(1, n) :
  
        # non- increasing order
        if a[i] <= a[i - 1] :
  
            p = (a[i - 1] - a[i]) // x + 1
  
            # add moves to answer 
            ans += p
  
            # increase the element
            a[i] += p * x
  
    # return required answer 
    return ans
          
# Driver code     
if __name__ == "__main__" :
  
    arr = [1, 3, 3, 2]
    x = 2
    n = len(arr)
  
    print(MinimumMoves(arr, n, x))
  
  
# This code is contributed by ANKITRAI1


C#
// C# program to find minimum moves required 
// to make the array in increasing order 
using System;
  
class GFG {
      
// function to find minimum moves required 
// to make the array in increasing order 
static int MinimumMoves(int[] a, int n, int x) 
{ 
      
    // to store answer 
    int ans = 0; 
      
    // iterate over an array 
    for (int i = 1; i < n; i++) { 
      
        // non- increasing order 
        if (a[i] <= a[i - 1]) { 
              
            int p = (a[i - 1] - a[i]) / x + 1; 
      
            // add moves to answer 
            ans += p; 
      
            // increase the element 
            a[i] += p * x; 
        } 
    } 
      
    // return required answer 
    return ans; 
} 
      
// Driver code 
public static void Main() 
{ 
      
    int[] arr = {1, 3, 3, 2}; 
    int x = 2; 
    int n = arr.Length; 
      
    Console.Write(MinimumMoves(arr, n, x)); 
      
} 
} 
  
// This code is contributed by ChitraNayal


PHP


输出:
3