Largest Permutation
https://practice.geeksforgeeks.org/problems/largest-permutation/0
Given a permutation of first n natural numbers as an array and an integer k. Print the lexicographically largest permutation after at most k swaps.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case contains two integers n and k where n denotes the number of elements in the array a[]. Next line contains space separated n elements in the array a[].
Output:
Print space separated n integers which form the largest permutation after at most k swaps.
Constraints:
1<=T<=100
1<=n<=1000
1<=a[i]<=1000
1<=k<=100 0
Example:
Input:
2
5 3
4 5 2 1 3
3 1
2 1 3
Output:
5 4 3 2 1
3 1 2
每次交换选择最大的到最前面。一次交换后可能影响后面的交换。
将数组从大到小排序 记录为 b[ ],与原数组对比,相同则不需交换。否则 对应的a[ i ] , b[ i ]交换。另需一个数组记录下标。
1 |
|