首页 > 其他 > 详细

G. Special Permutation(找规律模拟)

时间:2020-05-11 09:23:28      阅读:87      评论:0      收藏:0      [点我收藏+]

\(这题感觉自己就是瞎凑......\)

\(首先n<=3时无解\)

\(然后考虑奇数n\)

1 3 5 7 ...  n | n-3 n-1 | n-5 n-7 n-9 ... 2

\(其实就是前半段从1(奇数)一直加2加到n,就可以消去所有奇数\)

\(后半段从n-5(偶数)一直减2,就可以消去大部分偶数\)

\(至于中间的部分就是凑出来的\)

\(那么偶数其实也是一样的道理\)

\(前半段从1(奇数)一直加2加到n-1,后半段从n-6(偶数)一直减2\)

1 3 5 7 ... n-1 | n-4 n n-2 | n-6 n-8 ... 2
#include <bits/stdc++.h>
using namespace std;
int n,t,vis[1009];
int main()
{
	cin>>t;
	while(t--)
	{
		memset(vis,0,sizeof(vis));
		cin>>n;
		if(n<=3)	cout<<-1;
		else if(n==4)
			cout<<"2 4 1 3";
		else if(n%2==1)//奇数 
		{
			int now=1,shu=0;
			while(now<=n)	cout<<now<<" ",now+=2,shu++;
			now-=2;
			now-=3,cout<<now<<" ",shu++;//左三
			now+=2,cout<<now<<" ",shu++;//右二 
			if(shu<n) now-=4,cout<<now<<" ",shu++;//左四
			while(shu<n)	cout<<now-2<<" ",now-=2,shu++; 
		}
		else
		{
			int now=1,shu=0;
			while(now<=n)	cout<<now<<" ",now+=2,shu++;
			now-=2;
			if(shu<n) now-=3,cout<<now<<" ",shu++;//左三 
			if(shu<n) now+=4,cout<<now<<" ",shu++;//右四 
			if(shu<n) now-=2,cout<<now<<" ",shu++;//左二
			if(shu<n) now-=4,cout<<now<<" ",shu++;//左4 
			while(shu<n)	cout<<now-2<<" ",now-=2,shu++; 
		}
		cout<<endl;
	}
}

G. Special Permutation(找规律模拟)

原文:https://www.cnblogs.com/iss-ue/p/12866848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!