📌  相关文章
📜  通过将第 i 个元素移动到第 (i + B[i]) 个位置最少次数,对放置在数轴上的数组 A[] 的元素进行排序

📅  最后修改于: 2021-09-04 08:02:56             🧑  作者: Mango

给定两个由 N 个正整数组成的数组 A[]B[] ,使得每个数组元素A[i]都放在数轴上的第i 个位置,任务是找到对数组进行排序所需的最少操作次数数组元素排列在数轴上。在每次操作中,任何数组元素A[i]都可以移动到数轴上的第(i + B[i])个位置。两个或多个元素可以出现在同一条数轴上。

例子:

方法:可以使用贪心方法解决给定的问题,方法是将较大的元素一个一个地移动到下一个可能的索引,然后找到所需的所有操作中的最小值。请按照以下步骤解决给定的问题:

  • 初始化一个二维向量,比如arr[] ,这样每个i 个元素代表元素、相应的移动和当前位置为{arr[i], A[i], current_position}
  • 按升序对数组arr[]进行排序。
  • 初始化两个变量,比如cntf ,并将计数标记为0标记1以存储所需操作的数量。
  • 迭代直到F不等于1并执行以下步骤:
    • 更新 F的值等于0
    • 对于向量 arr[] 中的每个元素,如果arr[i][2] 的值至少为arr[i + 1][2] ,则将计数增加1f = 1( i + 1) th 个元素,即(arr[i + 1][2])arr[i + 1][1]
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum number of
// operations required to sort N array
// elements placed on a number line
int minHits(int arr[], int move[],
            int n)
{
    // Stores the value of
    // {A[i], B[i], current position}
    vector > V(n, vector(3));
 
    // Populate the current position
    // every elements
    for (int i = 0; i < n; i++) {
        V[i] = { arr[i], move[i], i };
    }
 
    // Sort the vector V
    sort(V.begin(), V.end());
 
    // Stores the total number of
    // operations required
    int cnt = 0;
    int f = 1;
 
    // Iterate until f equals 1
    while (f == 1) {
 
        // Update f equals zero
        f = 0;
 
        // Traverse through vector
        // and check for i and i+1
        for (int i = 0; i < n - 1; i++) {
 
            // If current position of
            // i is at least current
            // position of i+1
            if (V[i][2] >= V[i + 1][2]) {
 
                // Increase the current
                // position of V[i+1][2]
                // by V[i+1][1]
                V[i + 1][2] += V[i + 1][1];
 
                // Increment the count
                // of operations
                cnt++;
 
                // Update the flag equals
                // to 1
                f = 1;
 
                // Break the for loop
                break;
            }
        }
    }
 
    // Return the total operations
    // required
    return cnt;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 1, 4, 3 };
    int B[] = { 4, 1, 2, 4 };
    int N = sizeof(A) / sizeof(A[0]);
 
    cout << minHits(A, B, N);
 
    return 0;
}


Python3
# python 3 program for the above approach
 
# Function to find minimum number of
# operations required to sort N array
# elements placed on a number line
def minHits(arr, move, n):
    # Stores the value of
    # {A[i], B[i], current position}
    temp = [0 for i in range(3)]
    V = [temp for i in range(n)]
 
    # Populate the current position
    # every elements
    for i in range(n):
        V[i] = [arr[i], move[i], i]
 
    # Sort the vector V
    V.sort()
 
    # Stores the total number of
    # operations required
    cnt = 0
    f = 1
 
    # Iterate until f equals 1
    while(f == 1):
        # Update f equals zero
        f = 0
 
        # Traverse through vector
        # and check for i and i+1
        for i in range(n - 1):
            # If current position of
            # i is at least current
            # position of i+1
            if (V[i][2] >= V[i + 1][2]):
                # Increase the current
                # position of V[i+1][2]
                # by V[i+1][1]
                V[i + 1][2] += V[i + 1][1]
 
                # Increment the count
                # of operations
                cnt += 1
 
                # Update the flag equals
                # to 1
                f = 1
 
                # Break the for loop
                break
 
    # Return the total operations
    # required
    return cnt
 
# Driver Code
if __name__ == '__main__':
    A = [2, 1, 4, 3]
    B = [4, 1, 2, 4]
    N = len(A)
    print(minHits(A, B, N))
     
    # This code is contributed by bgangwar59.


输出:
5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live