题目链接:https://www.codechef.com/SEPT17/problems/MINPERM
求最小的错排
奇偶要分类讨论
偶数个时把
[1,n]
两两分组,每组两个交换,如
[1,2]
,
[3,4]
等
偶数个时前面一样,后面三个特殊处理,
n−2,n−1,n
变成
n−1,n,n−2
贴代码
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,Q;
int main(){
// freopen("2.in","r",stdin);
// freopen("2.out","w",stdout);
scanf("%d",&Q);
while (Q--){
scanf("%d",&n);
m=n%2==0?n:n-3;
for (int i=1;i<=m;i++)i%2==0?printf("%d ",i-1):printf("%d ",i+1);
if (n%2==1)printf("%d %d %d ",n-1,n,n-2);
printf("\n");
}
return 0;
}
【写的有漏洞的,欢迎路过大神吐槽】
2017/09/11 19:27:13
Ending.