显然排列构成若干个非自环环
故无解,其它情况先构造一个大小为
的环,剩下全构造大小为
的环即可。
#include <iostream>
using namespace std;
int n;
int k;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
if (k >= n) {
cout << "-1";
return 0;
}
if (k * 2 < n) {
cout << "-1";
return 0;
}
int firstLen = k * 2 - n + 2;
for (int i = 2; i <= firstLen; i++) {
cout << i << ' ';
}
cout << '1';
for (int i = firstLen + 1; i < n; i += 2) {
cout << ' ' << i + 1 << ' ' << i;
}
return 0;
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号