原文:

给定一组 n 个整数。任务是通过多次应用给定的操作来找到字典上最小的数组。操作是选取两个元素ait5】和ajt9】(1<= i,j < =n)使得ai ajt15】为奇数,然后互换ait19】和ajt23】。 举例:**

输入: a[] = {1,5,4,3,2} 输出: 1 2 3 4 5 解释:先换(5,2)再换(4,3)。这是通过 交换满足给定条件 输入: a[] = {4,2} 输出: 4 2 解释:不可能交换任何元素可以得到的 字典上最小的可能数组。

接近:观察两个元素如果奇偶性不同,是否有可能互换。如果数组中的所有元素具有相同的奇偶校验(奇数 奇数和偶数 偶数不是奇数),则不可能进行交换。因此,答案将只是输入数组。否则,您实际上可以交换任何一对元素。假设你想交换两个元素,a 和 b,它们具有相同的奇偶性。必须有第三个元素 c 具有不同的奇偶性。不失一般性,假设数组为【a,b,c】。让我们进行以下交换:

  • 交换 a 和 c:
  • 交换 b 和 c: [b,c,a]
  • 交换 a 和 c: [b,a,c]

换句话说,用 c 作为一个中间元素来交换 a 和 b,之后它总是会回到原来的位置。因为任何一对元素之间都可以交换,所以我们总是可以,这将是字典上最小的数组。 以下是上述办法的实施情况:

c

// cpp program to find possible
// lexicographically smaller array
// by swapping only elements whose sum is odd
#include 
using namespace std;
// function to find possible lexicographically smaller array
void lexicographically_smaller(int arr[], int n)
{
    // to store number of even and odd integers
    int odd = 0, even = 0;
    // find number of even and odd integers
    for (int i = 0; i < n; i  ) {
        if (arr[i] % 2)
            odd  ;
        else
            even  ;
    }
    // if it possible to make
    // lexicographically smaller
    if (odd && even)
        sort(arr, arr   n);
    // print the array
    for (int i = 0; i < n; i  )
        cout << arr[i] << " ";
}
// driver code
int main()
{
    int arr[] = { 1, 5, 4, 3, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    // function call
    lexicographically_smaller(arr, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find possible
// lexicographically smaller array
// by swapping only elements whose sum is odd
import java.util.*;
class gfg
{
// function to find possible lexicographically smaller array
static void lexicographically_smaller(int arr[], int n)
{
    // to store number of even and odd integers
    int odd = 0, even = 0;
    // find number of even and odd integers
    for (int i = 0; i < n; i  )
    {
        if (arr[i] % 2 == 1)
            odd  ;
        else
            even  ;
    }
    // if it possible to make
    // lexicographically smaller
    if (odd > 0 && even > 0)
        arrays.sort(arr);
    // print the array
    for (int i = 0; i < n; i  )
        system.out.print(arr[i]   " ");
}
// driver code
public static void main(string[] args)
{
    int arr[] = { 1, 5, 4, 3, 2 };
    int n = arr.length;
    // function call
    lexicographically_smaller(arr, n);
}
}
// this code is contributed by rajput-ji

python 3

# python3 program to find possible
# lexicographically smaller array
# by swapping only elements whose sum is odd
# function to find possible
# lexicographically smaller array
def lexicographically_smaller(arr, n):
    # to store number of even and odd integers
    odd, even = 0, 0;
    # find number of even and odd integers
    for i in range(n):
        if (arr[i] % 2 == 1):
            odd  = 1;
        else:
            even  = 1;
    # if it possible to make
    # lexicographically smaller
    if (odd > 0 and even > 0):
        arr.sort();
    # print the array
    for i in range(n):
        print(arr[i], end = " ");
# driver code
if __name__ == '__main__':
    arr = [ 1, 5, 4, 3, 2 ];
    n = len(arr);
    # function call
    lexicographically_smaller(arr, n);
# this code contributed by rajput-ji

c

// c# program to find possible
// lexicographically smaller array by
// swapping only elements whose sum is odd
using system;
class gfg
{
// function to find possible
// lexicographically smaller array
static void lexicographically_smaller(int []arr, int n)
{
    // to store number of even and odd integers
    int odd = 0, even = 0;
    // find number of even and odd integers
    for (int i = 0; i < n; i  )
    {
        if (arr[i] % 2 == 1)
            odd  ;
        else
            even  ;
    }
    // if it possible to make
    // lexicographically smaller
    if (odd > 0 && even > 0)
        array.sort(arr);
    // print the array
    for (int i = 0; i < n; i  )
        console.write(arr[i]   " ");
}
// driver code
public static void main(string[] args)
{
    int []arr = { 1, 5, 4, 3, 2 };
    int n = arr.length;
    // function call
    lexicographically_smaller(arr, n);
}
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

1 2 3 4 5

时间复杂度 : o(n log n)