📌  相关文章
📜  通过交换另一个数组指定的不同类型的元素对数组进行排序

📅  最后修改于: 2021-05-17 17:41:49             🧑  作者: Mango

给定两个分别包含整数元素及其各自类型(类型0或类型1)的数组a []和b [] ,任务是检查是否有可能通过交换元素以非降序对数组进行排序仅限不同类型。
例子:

方法:
为了解决上述问题,需要注意以下几点:

  • 如果数组a []已经以非降序排序,那么答案是
  • 如果数组a []没有排序,并且所有元素都属于同一类型,则答案为“否”,因为不可能进行交换。
  • 在其他情况下,答案是肯定的,因为我们可以随时对数组进行排序。这是因为我们将至少拥有一个其类型与其他元素不同的元素,因此我们可以与所有其他元素进行多次交换,直到所有元素都处于其排序位置。

下面是上述方法的实现:

C++
// C++ program to check if it
// is possible to sort the
// array in non-decreasing
// order by swapping
// elements of different types
 
#include 
using namespace std;
 
// Function to check if it is
// possible to sort the array
// in non-decreasing order by
// swapping elements of
// different types
bool sorting_possible(int a[],
                      int b[], int n)
{
    // Consider the array
    // to be already sorted
    bool sorted = true;
 
    int type1 = 0, type0 = 0, i;
 
    // checking if array is
    // already sorted
    for (i = 1; i < n; i++) {
        // Check for a pair which
        // is in decreasing order
        if (a[i] < a[i - 1]) {
 
            sorted = false;
            break;
        }
    }
 
    // Count the frequency of
    // each type of element
    for (i = 0; i < n; i++) {
        // type0 stores count
        // of elements of type 0
        if (b[i] == 0)
            type0++;
 
        // type1 stores count
        // of elements of type 1
        else
            type1++;
    }
 
    // Return true if array
    // is already sorted
    if (sorted)
        return true;
 
    // Return false if all
    // elements are of same
    // type and array
    // is unsorted
    else if (type1 == n
             || type0 == n)
        return false;
 
    // Possible for all
    // other cases
    else
        return true;
}
 
// Driver Program
int main()
{
    int a[] = { 15, 1, 2, 17, 6 };
    int b[] = { 1, 1, 0, 1, 1 };
    int n = sizeof(a) / sizeof(a[0]);
 
    bool res = sorting_possible(a, b, n);
 
    if (res)
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program to check if it is
// possible to sort the array in
// non-decreasing order by swapping
// elements of different types
import java.util.*;
 
class GFG{
 
// Function to check if it is
// possible to sort the array
// in non-decreasing order by
// swapping elements of
// different types
static boolean sorting_possible(int a[],
                                int b[],
                                int n)
{
     
    // Consider the array
    // to be already sorted
    boolean sorted = true;
 
    int type1 = 0, type0 = 0, i;
 
    // Checking if array is
    // already sorted
    for(i = 1; i < n; i++)
    {
         
       // Check for a pair which
       // is in decreasing order
       if (a[i] < a[i - 1])
       {
           sorted = false;
           break;
       }
    }
 
    // Count the frequency of
    // each type of element
    for(i = 0; i < n; i++)
    {
         
       // type0 stores count
       // of elements of type 0
       if (b[i] == 0)
           type0++;
      
       // type1 stores count
       // of elements of type 1
       else
           type1++;
    }
 
    // Return true if array
    // is already sorted
    if (sorted)
        return true;
 
    // Return false if all elements
    // are of same type and array
    // is unsorted
    else if (type1 == n || type0 == n)
        return false;
 
    // Possible for all
    // other cases
    else
        return true;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 15, 1, 2, 17, 6 };
    int b[] = { 1, 1, 0, 1, 1 };
    int n = a.length;
     
    boolean res = sorting_possible(a, b, n);
 
    if (res)
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to check if it
# is possible to sort the
# array in non-decreasing
# order by swapping
# elements of different types
 
# Function to check if it is
# possible to sort the array
# in non-decreasing order by
# swapping elements of different types
def sorting_possible(a, b, n):
     
    # Consider the array
    # to be already sorted
    sorted = True
     
    type1 = 0
    type0 = 0
     
    # Checking if array is
    # already sorted
    for i in range(1, n):
         
        # Check for a pair which
        # is in decreasing order
        if (a[i] < a[i - 1]):
            sorted = False
            break
     
    # Count the frequency of
    # each type of element
    for i in range(n):
         
        # type0 stores count
        # of elements of type 0
        if (b[i] == 0):
            type0 += 1
         
        # type1 stores count
        # of elements of type 1
        else:
            type1 += 1
     
    # Return true if array
    # is already sorted
    if (sorted != False):
        return True
     
    # Return false if all elements
    # are of same type and array
    # is unsorted
    elif (type1 == n or type0 == n):
        return False
         
    # Possible for all
    # other cases
    else:
        return True
     
# Driver code
a = [ 15, 1, 2, 17, 6 ]
b = [ 1, 1, 0, 1, 1 ]
 
n = len(a)
res = sorting_possible(a, b, n)
 
if (res != False):
    print("Yes")
else:
    print("No")
     
# This code is contributed by sanjoy_62


C#
// C# program to check if it is
// possible to sort the array in
// non-decreasing order by swapping
// elements of different types
using System;
 
class GFG{
 
// Function to check if it is
// possible to sort the array
// in non-decreasing order by
// swapping elements of
// different types
static bool sorting_possible(int []a,
                             int []b,
                             int n)
{
     
    // Consider the array
    // to be already sorted
    bool sorted = true;
 
    int type1 = 0, type0 = 0, i;
 
    // Checking if array is
    // already sorted
    for(i = 1; i < n; i++)
    {
        
       // Check for a pair which
       // is in decreasing order
       if (a[i] < a[i - 1])
       {
           sorted = false;
           break;
       }
    }
 
    // Count the frequency of
    // each type of element
    for(i = 0; i < n; i++)
    {
        
       // type0 stores count
       // of elements of type 0
       if (b[i] == 0)
           type0++;
            
       // type1 stores count
       // of elements of type 1
       else
           type1++;
    }
 
    // Return true if array
    // is already sorted
    if (sorted)
        return true;
 
    // Return false if all elements
    // are of same type and array
    // is unsorted
    else if (type1 == n || type0 == n)
        return false;
 
    // Possible for all
    // other cases
    else
        return true;
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 15, 1, 2, 17, 6 };
    int []b = { 1, 1, 0, 1, 1 };
    int n = a.Length;
     
    bool res = sorting_possible(a, b, n);
 
    if (res)
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes

插图: