📌  相关文章
📜  最小化给定数组之间相应索引处不相等元素的数量

📅  最后修改于: 2021-09-06 06:51:31             🧑  作者: Mango

给定两个由N 个正整数组成的数组A[]B[]和一个由M对索引组成的矩阵List[][] ,任务是最小化不相等的相同索引元素的数量( A i != B i ) 通过在数组 A[] 中的任何给定索引对之间交换来从两个数组中提取。

例子:

方法:由于在对列表中指定的数组 A[] 的元素可以交换任意次数,因此可以将这些元素视为连接集,允许在其元素内进行交换。以下是实现此方法的步骤:

  1. 遍历给定的数组 A[] 并创建一组可以使用不相交集轻松完成的连接组件。
  2. 对于B[] 中的每个元素,找出A[] (A[i], B[i]) 中对应的元素是否属于同一个连通分量。
  3. 如果是,使 A[i]=B[i] 并继续。否则,增加不匹配对的计数。
  4. 在所有上述步骤之后打印不匹配对的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
int find(int par[],int x);
void unionn(int par[], int a, int b);
 
// Function that count of the mismatched
// pairs in bot the array
int countPairs(int A[], int B[], int N,
               int M, int List[][2])
{
    int count = 0;
 
    // Create a parent array
    // and initialize it
    int par[N + 1];
    for(int i = 0; i <= N; i++)
        par[i] = i;
 
    // Preprocessing of the given
    // pairs of indices
    for(int i = 0; i < M; i++)
    {
         
        // 1-based indexing
        int index1 = find(par, List[i][0] - 1);
        int index2 = find(par, List[i][1] - 1);
 
        // If both indices doesn't belong
        // to same component
        if (index1 != index2)
        {
             
            // Insert the indices in
            // same component
            unionn(par, index1, index2);
        }
    }
 
    // Map to get the indices
    // of array A
    map mp;
 
    for(int i = 0; i < N; i++)
    {
        mp[A[i]] = i;
    }
 
    for(int i = 0; i < N; i++)
    {
        if (A[i] != B[i])
        {
 
            // If current element is not
            // present in array B then
            // count this as mismatched
            if(mp.find(B[i]) == mp.end())
            {
                count++;
                continue;
            }
             
            // Get the index of the element
            // in array B
            int j = mp[B[i]];
 
            // Check if both indices belong
            // to same connected component
            // if not increment the count
            if (find(par, i) != find(par, j))
                count++;
        }
    }
     
    // Return answer
    return count;
}
 
// Function that gives the connected
// component of each index
int find(int par[],int x)
{
    if (par[x] == x)
        return x;
    else
        return par[x] = find(par, par[x]);
}
 
// Function that creates the
// connected componenets
void unionn(int par[], int a, int b)
{
     
    // Find parent of a and b
    // recursively
    a = find(par, a);
    b = find(par, b);
     
    if (a == b)
        return;
 
    // Update the parent of a
    par[a] = b;
}
 
// Driver Code
int main()
{
    int N = 5;
    int M = 4;
 
    // Given arrays A[], B[]
    int A[] = { 1, 5, 9, 2, 3 };
    int B[] = { 2, 4, 5, 1, 3 };
 
    // List of indices
    int List[][2] = { { 1, 4 }, { 2, 3 },
                      { 3, 5 }, { 2, 5 } };
 
    // Function call
    cout << countPairs(A, B, N, M, List);
 
    return 0;
}
 
// This code is contributed by rutvik_56


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function that count of the mismatched
    // pairs in bot the array
    public static int countPairs(int A[], int B[],
                                int N, int M,
                                int[][] List)
    {
        int count = 0;
 
        // Create a parent array
        // and initialize it
        int par[] = new int[N + 1];
        for (int i = 0; i <= N; i++)
            par[i] = i;
 
        // Preprocessing of the given
        // pairs of indices
        for (int i = 0; i < M; i++) {
 
            // 1-based indexing
            int index1
                = find(par, List[i][0] - 1);
            int index2
                = find(par, List[i][1] - 1);
 
            // If both indices doesn't belong
            // to same component
            if (index1 != index2) {
 
                // Insert the indices in
                // same component
                union(par, index1, index2);
            }
        }
 
        // HashMap to get the indices
        // of array A
        HashMap map
            = new HashMap<>();
 
        for (int i = 0; i < N; i++) {
            map.put(A[i], i);
        }
 
        for (int i = 0; i < N; i++) {
            if (A[i] != B[i]) {
 
                // If current element is not
                // present in array B then
                // count this as mismatched
                if (!map.containsKey(B[i])) {
                    count++;
 
                    continue;
                }
 
                // Get the index of the element
                // in array B
                int j = map.get(B[i]);
 
                // Check if both indices belong
                // to same connected component
                // if not increment the count
                if (find(par, i) != find(par, j))
                    count++;
            }
        }
 
        // Return answer
        return count;
    }
 
    // Function that gives the connected
    // component of each index
    public static int find(int par[],
                        int x)
    {
        if (par[x] == x)
            return x;
        else
            return par[x]
                = find(par, par[x]);
    }
 
    // Function that creates the
    // connected componenets
    public static void
    union(int par[], int a, int b)
    {
        // Find parent of a and b
        // recursively
        a = find(par, a);
        b = find(par, b);
        if (a == b)
            return;
 
        // Update the parent of a
        par[a] = b;
    }
 
    // Driver Code
    public static void
        main(String[] args)
    {
        int N = 5;
        int M = 4;
 
        // Given arrays A[], B[]
        int A[] = { 1, 5, 9, 2, 3 };
        int B[] = { 2, 4, 5, 1, 3 };
 
        // List of indices
        int List[][]
            = { { 1, 4 }, { 2, 3 },
                { 3, 5 }, { 2, 5 } };
 
        // Function Call
        System.out.println(
            countPairs(A, B, N, M, List));
    }
}


Python3
# Python3 program for the above approach
 
# Function that count of the mismatched
# pairs in bot the array
def countPairs(A, B, N, M, List):
 
    count = 0
 
    # Create a parent array
    # and initialize it
    par = [0] * (N + 1)
    for i in range(N + 1):
        par[i] = i
 
    # Preprocessing of the given
    # pairs of indices
    for i in range(M):
 
        # 1-based indexing
        index1 = find(par, List[i][0] - 1)
        index2 = find(par, List[i][1] - 1)
 
        # If both indices doesn't belong
        # to same component
        if(index1 != index2):
 
            # Insert the indices in
            # same component
            union(par, index1, index2)
 
    # HashMap to get the indices
    # of array A
    map = {}
 
    for i in range(N):
        map[A[i]] = i
 
    for i in range(N):
        if(A[i] != B[i]):
 
            # If current element is not
            # present in array B then
            # count this as mismatched
            if(B[i] not in map.keys()):
                count += 1
                continue
 
            # Get the index of the element
            # in array B
            j = map[B[i]]
 
            # Check if both indices belong
            # to same connected component
            # if not increment the count
            if(find(par, i) != find(par, j)):
                count += 1
 
    # Return answer
    return count
 
# Function that gives the connected
# component of each index
def find(par, x):
 
    if(par[x] == x):
        return x
    else:
        par[x] = find(par, par[x])
        return par[x]
 
# Function that creates the
# connected componenets
def union(par, a, b):
 
    # Find parent of a and b
    # recursively
    a = find(par, a)
    b = find(par, b)
 
    if(a == b):
        return
 
    # Update the parent of a
    par[a] = b
 
# Driver Code
N = 5
M = 4
 
# Given arrays A[], B[]
A = [ 1, 5, 9, 2, 3 ]
B = [ 2, 4, 5, 1, 3 ]
 
# List of indices
List = [ [ 1, 4 ], [ 2, 3 ],
        [ 3, 5 ], [ 2, 5 ] ]
 
# Function call
print(countPairs(A, B, N, M, List))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
 
    // Function that count of the mismatched
    // pairs in bot the array
    public static int countPairs(int[] A, int[] B, int N,
                                int M, int[, ] List)
    {
        int count = 0;
 
        // Create a parent array
        // and initialize it
        int[] par = new int[N + 1];
        for (int i = 0; i <= N; i++)
            par[i] = i;
 
        // Preprocessing of the given
        // pairs of indices
        for (int i = 0; i < M; i++)
        {
 
            // 1-based indexing
            int index1 = find(par, List[i, 0] - 1);
            int index2 = find(par, List[i, 1] - 1);
 
            // If both indices doesn't belong
            // to same component
            if (index1 != index2)
            {
 
                // Insert the indices in
                // same component
                union(par, index1, index2);
            }
        }
 
        // Dictionary to get the indices
        // of array A
        Dictionary map = new Dictionary();
        for (int i = 0; i < N; i++)
        {
            if (map.ContainsKey(A[i]))
                map[A[i]] = i;
            else
                map.Add(A[i], i);
        }
 
        for (int i = 0; i < N; i++)
        {
            if (A[i] != B[i])
            {
 
                // If current element is not
                // present in array B then
                // count this as mismatched
                if (!map.ContainsKey(B[i]))
                {
                    count++;
                    continue;
                }
 
                // Get the index of the element
                // in array B
                int j = map[B[i]];
 
                // Check if both indices belong
                // to same connected component
                // if not increment the count
                if (find(par, i) != find(par, j))
                    count++;
            }
        }
 
        // Return answer
        return count;
    }
 
    // Function that gives the connected
    // component of each index
    public static int find(int[] par, int x)
    {
        if (par[x] == x)
            return x;
        else
            return par[x] = find(par, par[x]);
    }
 
    // Function that creates the
    // connected componenets
    public static void union(int[] par, int a, int b)
    {
        // Find parent of a and b
        // recursively
        a = find(par, a);
        b = find(par, b);
        if (a == b)
            return;
 
        // Update the parent of a
        par[a] = b;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 5;
        int M = 4;
 
        // Given arrays []A, []B
        int[] A = {1, 5, 9, 2, 3};
        int[] B = {2, 4, 5, 1, 3};
 
        // List of indices
        int[, ] List = {{1, 4}, {2, 3}, {3, 5}, {2, 5}};
 
        // Function Call
        Console.WriteLine(countPairs(A, B, N, M, List));
    }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
1

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

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