题解:[ARC144C] K Derangement
Tendou_Eous · · 题解
我是打表仙人。显而易见的有当 6 3 的输出结果:4 5 6 1 2 3。再让我们试试 12 3 的输出结果:4 5 6 1 2 3 10 11 12 7 8 9。是不是有点初见端倪?再来一组:8 2 的输出:3 4 1 2 7 8 5 6。现在你可以看出规律了。把
其中
那么 8 3。输出为 4 5 6 7 8 1 2。再来 10 3。输出为 4 5 6 1 8 9 10 2 3 7。多打几组,你会发现,前面
code:
#include<bits/extc++.h>
#define int long long
using namespace std;
const int maxn = 3e5 + 5;
int n,k;
int p[maxn];
bool b[maxn];
vector<int>ans;
signed main()
{
scanf("%lld%lld",&n,&k);
if ((k << 1) > n)
return printf("-1"),0;
fill(b + 1,b + n + 1,1);
for (int s = 1; s + (k << 1) - 1 <= n && ans.size() < n - (k << 1); s += (k << 1))
{
int t = s + (k << 1);
for (int i = s + k; i < t && ans.size() < n - (k << 1); i++)
{
b[i] = 0;
ans.push_back(i);
}
for (int i = s; i < s + k && ans.size() < n - (k << 1); i++)
{
b[i] = 0;
ans.push_back(i);
}
}
for (int i = n - k + 1; i <= n; i++)
{
b[i] = 0;
ans.push_back(i);
}
for (int i = 1; i <= n; i++)
if (b[i])
ans.push_back(i);
for (auto i : ans)
printf("%lld ",i);
return 0;
}