/*
Time:2019.11.8
Author: Goven
type:全排列
err:超时 Time Limit Exceeded
ref: https://www.cnblogs.com/coolwind-sea/archive/2012/04/17/2454313.html
https://blog.csdn.net/qq_40421671/article/details/89073418
知识点 :1.next_permutation()到达最后一个排列组合时,再调用会返回false,但是组合会重新循环
时间效率不高
2.输出用copy,效率较高
*/
//错误版--超时
#include<iostream>
#include<algorithm>
using namespace std;
int a[1050];
int main()
{
int t, n, k;
cin >> t;
while (t--) {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
while (k && next_permutation(a, a + n)) k--;
if (k > 0) {
sort(a, a + n);
k--;
while (k && next_permutation(a, a + n)) k--;
}
cout << a[0];
for (int i = 1; i < n; i++) {
cout << " " << a[i];
}
cout << endl;
}
return 0;
}
//正确
#include<iostream>
#include<algorithm>
#include<iterator>
using namespace std;
int a[1050];
int main()
{
int t, n, k;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
// cin >> a[i];//err1
scanf("%d", &a[i]);
}
for (int i = 0; i < k ; i++) {
next_permutation(a, a + n);
}
copy(a, a + n - 1, ostream_iterator<int>(cout, " "));
cout << a[n - 1] << endl;
}
return 0;
}
网友评论