📜  在给定条件下将一个排列转换为另一个排列所需的最小相邻交换数

📅  最后修改于: 2021-06-25 21:03:21             🧑  作者: Mango

给定大小为N置换P ,其值从1到N。任务是找到所需的最小相邻交换数,以使得对于[1,N]范围内的所有i, P [i]不等于i
例子:

方法:让我们考虑用X表示P [i] = i的位置,用O表示其他位置。以下是对该问题的三个基本观察:

  • 如果排列的任意两个相邻索引处的值的格式为XO ,则我们可以简单地交换这两个索引以获取’OO’。
  • 如果该排列的任意两个相邻索引处的值的格式为XX ,则我们可以简单地交换这两个索引以获取’OO’。
  • 如果排列的任意两个相邻索引处的值的格式为OX ,则一旦指针到达X处的索引,它的名称就是‘XO’‘XX’

步骤如下:

  1. 1迭代到N – 1并检查P [i] = i,然后我们简单地交换(P [i],P [i + 1]) ,否则继续下一个相邻对的处理。
  2. 给定问题的极端案例是当i = N时,如果P [i] = i,则我们交换(P [i],P [i – 1])

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// number of swaps
void solve(vector& P, int n)
{
 
    // New array to convert
    // to 1-based indexing
    vector arr;
 
    arr.push_back(0);
 
    for (auto x : P)
        arr.push_back(x);
 
    // Keeps count of swaps
    int cnt = 0;
 
    for (int i = 1; i < n; i++) {
 
        // Check if it is an 'X' position
        if (arr[i] == i) {
            swap(arr[i], arr[i + 1]);
            cnt++;
        }
    }
 
    // Corner Case
    if (arr[n] == n) {
 
        swap(arr[n - 1], arr[n]);
        cnt++;
    }
 
    // Print the minimum swaps
    cout << cnt << endl;
}
 
// Driver Code
signed main()
{
    // Given Number N
    int N = 9;
 
    // Given Permutation of N numbers
    vector P = { 1, 2, 4, 9, 5,
                      8, 7, 3, 6 };
 
    // Function Call
    solve(P, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the minimum
// number of swaps
static void solve(int P[], int n)
{
 
    // New array to convert
    // to 1-based indexing
    int arr[] = new int[n + 1];
 
    arr[0] = 0;
 
    for(int i = 0; i < n; i++)
       arr[i + 1] = P[i];
 
    // Keeps count of swaps
    int cnt = 0;
 
    for(int i = 1; i < n; i++)
    {
        
       // Check if it is an 'X' position
       if (arr[i] == i)
       {
           int t = arr[i + 1];
           arr[i + 1] = arr[i];
           arr[i] = t;
           cnt++;
       }
    }
 
    // Corner Case
    if (arr[n] == n)
    {
         
        // Swap
        int t = arr[n - 1];
        arr[n - 1] = arr[n];
        arr[n] = t;
        cnt++;
    }
 
    // Print the minimum swaps
    System.out.println(cnt);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Number N
    int N = 9;
 
    // Given Permutation of N numbers
    int P[] = new int[]{ 1, 2, 4, 9, 5,
                         8, 7, 3, 6 };
 
    // Function Call
    solve(P, N);
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# number of swaps
def solve(P, n):
 
    # New array to convert
    # to 1-based indexing
    arr = []
 
    arr.append(0)
 
    for x in P:
        arr.append(x)
 
    # Keeps count of swaps
    cnt = 0
 
    for i in range(1, n):
 
        # Check if it is an 'X' position
        if (arr[i] == i):
            arr[i], arr[i + 1] = arr[i + 1], arr[i]
            cnt += 1
 
    # Corner Case
    if (arr[n] == n):
        arr[n - 1], arr[n] = arr[n] , arr[n - 1]
        cnt += 1
 
    # Print the minimum swaps
    print(cnt)
 
# Driver Code
 
# Given number N
N = 9
 
# Given permutation of N numbers
P = [ 1, 2, 4, 9, 5,
      8, 7, 3, 6 ]
 
# Function call
solve(P, N)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum
// number of swaps
static void solve(int []P, int n)
{
     
    // New array to convert
    // to 1-based indexing
    int []arr = new int[n + 1];
 
    arr[0] = 0;
 
    for(int i = 0; i < n; i++)
        arr[i + 1] = P[i];
 
    // Keeps count of swaps
    int cnt = 0;
 
    for(int i = 1; i < n; i++)
    {
         
        // Check if it is an 'X' position
        if (arr[i] == i)
        {
            int t = arr[i + 1];
            arr[i + 1] = arr[i];
            arr[i] = t;
            cnt++;
        }
    }
 
    // Corner Case
    if (arr[n] == n)
    {
         
        // Swap
        int t = arr[n - 1];
        arr[n - 1] = arr[n];
        arr[n] = t;
        cnt++;
    }
 
    // Print the minimum swaps
    Console.WriteLine(cnt);
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given Number N
    int N = 9;
 
    // Given Permutation of N numbers
    int []P = { 1, 2, 4, 9, 5,
                8, 7, 3, 6 };
 
    // Function Call
    solve(P, N);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
3

时间复杂度: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。