B. Permutation(思维+构造) Codeforces Round #209 (Div. 2)
原题链接: https://codeforces.com/problemset/problem/359/B

测试样例
Input
1 0
Output
1 2
Input
2 1
Output
3 2 1 4
Input
4 0
Output
2 7 4 6 1 3 5 8
题意: 给你
2
2
2个正整数
n
n
n和
k
k
k,其中
2
⋅
k
≤
n
2\cdot k\leq n
2⋅k≤n,你需要找到
1
1
1~
4
⋅
n
4\cdot n
4⋅n的排列数组,使得

。
解题思路: 由于 2 ⋅ k ≤ n 2\cdot k\leq n 2⋅k≤n,所以我们如果以 1 1 1为相邻之差的话自然也是可以达成任务需求的,所以按照 k k k的值一次模拟编排即可。
AC代码
/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>//POJ不支持
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=a;i>=n;i--)
using namespace std;
const int inf=0x3f3f3f3f;//无穷大。
const int maxn=1e5;//限定值。
typedef long long ll;
int n,k;
int main(){
while(cin>>n>>k){
//所有绝对值相加得到的与算数值相加再加+2k
int temp=2*k;
rep(i,1,2*n){
if(temp>=2){
cout<<i<<" "<<i+1<<" ";
temp-=2;
i++;
}
else{
cout<<i+1<<" "<<i<<" ";
i++;
}
}
cout<<endl;
}
return 0;
}

浙公网安备 33010602011771号