特判只有一种情况的。
对于其他情况,发现一定被统计两次,
一定被统计一次,其他数可以被统计零或一次。故有解的充要条件是
。
当有解时,我们可以将放在最右侧,从大到小贪心选择左侧序列,然后将剩余的数放在
和
之间即可。
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
int n;
ll k;
void Solve() {
cin >> n >> k;
if (n == 1) {
if (k != 2) {
cout << "-1";
return;
}
cout << '1';
return;
}
int mini = n * 3 - 1;
ll maxi = (ll)n * (n + 3) / 2;
if (k < mini || k > maxi) {
cout << "-1";
return;
}
vector<bool> vis(n + 1);
k -= n * 2;
vector<int> res;
for (int i = n - 1; k; i--) {
if (k >= i) {
k -= i;
res.push_back(i);
vis[i] = true;
}
}
reverse(res.begin(), res.end());
for (const auto& x : res) {
cout << x << ' ';
}
for (int i = 1; i < n; i++) {
if (!vis[i]) {
cout << i << ' ';
}
}
cout << n;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Solve();
return 0;
}

京公网安备 11010502036488号