原文:

给定一个包含 n 整数的数组 arr[] ,任务是打印 k 不同的索引排列,使得这些索引处的值形成一个非递减序列。如果不可能,打印 -1

示例:

输入: arr[] = {1,3,3,1},k = 3 输出: 0 3 1 2 3 0 1 2 3 0 2 1 对于每个排列,索引处的值形成以下序列{1,1,3,3} 输入: arr[] = {1,2,3,4},k = 3 输出:-1【t11

方法:对给定的数组进行排序,并跟踪每个元素的原始索引。这给出了一个必要的排列。如果任意两个连续的元素相等,那么它们可以交换,得到另一个排列。类似地,可以生成第三置换。

下面是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
int next_pos = 1;
// utility function to print the original indices
// of the elements of the array
void printindices(int n, pair a[])
{
    for (int i = 0; i < n; i  )
        cout << a[i].second << " ";
    cout << endl;
}
// function to print the required permutations
void printpermutations(int n, int a[], int k)
{
    // to keep track of original indices
    pair arr[n];
    for (int i = 0; i < n; i  )
    {
        arr[i].first = a[i];
        arr[i].second = i;
    }
    // sort the array
    sort(arr, arr   n);
    // count the number of swaps that can
    // be made
    int count = 1;
    for (int i = 1; i < n; i  )
        if (arr[i].first == arr[i - 1].first)
            count  ;
    // cannot generate 3 permutations
    if (count < k) {
        cout << "-1";
        return;
    }
    for (int i = 0; i < k - 1; i  )
    {
        // print the first permutation
        printindices(n, arr);
        // find an index to swap and create
        // second permutation
        for (int j = next_pos; j < n; j  )
        {
            if (arr[j].first == arr[j - 1].first)
            {
                swap(arr[j], arr[j - 1]);
                next_pos = j   1;
                break;
            }
        }
    }
    // print the last permutation
    printindices(n, arr);
}
// driver code
int main()
{
    int a[] = { 1, 3, 3, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 3;
    // function call
    printpermutations(n, a, k);
    return 0;
}

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

// java implementation of the approach
import java.util.*;
class gfg {
    static int next_pos = 1;
    static class pair {
        int first, second;
        pair()
        {
            first = 0;
            second = 0;
        }
    }
    // utility function to print the original indices
    // of the elements of the array
    static void printindices(int n, pair a[])
    {
        for (int i = 0; i < n; i  )
            system.out.print(a[i].second   " ");
        system.out.println();
    }
    static class sort implements comparator
    {
        // used for sorting in ascending order
        public int compare(pair a, pair b)
        {
            return a.first < b.first ? -1 : 1;
        }
    }
    // function to print the required permutations
    static void printpermutations(int n, int a[], int k)
    {
        // to keep track of original indices
        pair arr[] = new pair[n];
        for (int i = 0; i < n; i  )
        {
            arr[i] = new pair();
            arr[i].first = a[i];
            arr[i].second = i;
        }
        // sort the array
        arrays.sort(arr, new sort());
        // count the number of swaps that can
        // be made
        int count = 1;
        for (int i = 1; i < n; i  )
            if (arr[i].first == arr[i - 1].first)
                count  ;
        // cannot generate 3 permutations
        if (count < k)
        {
            system.out.print("-1");
            return;
        }
        for (int i = 0; i < k - 1; i  )
        {
            // print the first permutation
            printindices(n, arr);
            // find an index to swap and create
            // second permutation
            for (int j = next_pos; j < n; j  )
            {
                if (arr[j].first == arr[j - 1].first)
                {
                    pair t = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = t;
                    next_pos = j   1;
                    break;
                }
            }
        }
        // print the last permutation
        printindices(n, arr);
    }
    // driver code
    public static void main(string arsg[])
    {
        int a[] = { 1, 3, 3, 1 };
        int n = a.length;
        int k = 3;
        // function call
        printpermutations(n, a, k);
    }
}
// this code is contributed by arnab kundu

python 3

# python 3 implementation of the approach
# utility function to print the original
# indices of the elements of the array
def printindices(n, a):
    for i in range(n):
        print(a[i][1], end=" ")
    print("\n", end="")
# function to print the required
# permutations
def printpermutations(n, a, k):
    # to keep track of original indices
    arr = [[0, 0] for i in range(n)]
    for i in range(n):
        arr[i][0] = a[i]
        arr[i][1] = i
    # sort the array
    arr.sort(reverse=false)
    # count the number of swaps that
    # can be made
    count = 1
    for i in range(1, n):
        if (arr[i][0] == arr[i - 1][0]):
            count  = 1
    # cannot generate 3 permutations
    if (count < k):
        print("-1", end="")
        return
    next_pos = 1
    for i in range(k - 1):
        # print the first permutation
        printindices(n, arr)
        # find an index to swap and create
        # second permutation
        for j in range(next_pos, n):
            if (arr[j][0] == arr[j - 1][0]):
                temp = arr[j]
                arr[j] = arr[j - 1]
                arr[j - 1] = temp
                next_pos = j   1
                break
    # print the last permutation
    printindices(n, arr)
# driver code
if __name__ == '__main__':
    a = [1, 3, 3, 1]
    n = len(a)
    k = 3
    # function call
    printpermutations(n, a, k)
# this code is contributed by
# surendra_gangwar

c

// c# implementation of the approach
using system;
using system.collections;
using system.collections.generic;
class gfg {
  static int next_pos = 1;
  public class pair {
    public int first, second;
    public pair()
    {
      first = 0;
      second = 0;
    }
  }
  class sorthelper : icomparer
  {
    int icomparer.compare(object a, object b)
    {
      pair first = (pair)a;
      pair second = (pair)b;
      return first.first < second.first ? -1 : 1;
    }
  }
  // utility function to print the original indices
  // of the elements of the array
  static void printindices(int n, pair []a)
  {
    for (int i = 0; i < n; i  )
      console.write(a[i].second   " ");
    console.writeline();
  }
  // function to print the required permutations
  static void printpermutations(int n, int []a, int k)
  {
    // to keep track of original indices
    pair []arr = new pair[n];
    for (int i = 0; i < n; i  )
    {
      arr[i] = new pair();
      arr[i].first = a[i];
      arr[i].second = i;
    }
    // sort the array
    array.sort(arr, new sorthelper());
    // count the number of swaps that can
    // be made
    int count = 1;
    for (int i = 1; i < n; i  )
      if (arr[i].first == arr[i - 1].first)
        count  ;
    // cannot generate 3 permutations
    if (count < k)
    {
      console.write("-1");
      return;
    }
    for (int i = 0; i < k - 1; i  )
    {
      // print the first permutation
      printindices(n, arr);
      // find an index to swap and create
      // second permutation
      for (int j = next_pos; j < n; j  )
      {
        if (arr[j].first == arr[j - 1].first)
        {
          pair t = arr[j];
          arr[j] = arr[j - 1];
          arr[j - 1] = t;
          next_pos = j   1;
          break;
        }
      }
    }
    // print the last permutation
    printindices(n, arr);
  }
  // driver code
  public static void main(string []args)
  {
    int []a = { 1, 3, 3, 1 };
    int n = a.length;
    int k = 3;
    // function call
    printpermutations(n, a, k);
  }
}
// this code is contributed by rutvik_56.

java 描述语言


output

0 3 1 2 
3 0 1 2 
3 0 2 1 

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