G. Special Permutation(构造)

题目传送门

题意: 要求你生成一个排列,要求任意两个相邻的数的差值的绝对值在[2,4]范围内。

思路: 四个一循环,可以一直循环,然后最后一组四个和最后剩下的特判输出就可以。

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
//#define int long long
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=8e3+5;
const int inf=0x7fffffff;
const int mod=998244353;
const double eps=1e-6;
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        if(n<=3)
        {
            cout<<-1<<endl;
            continue;
        }
        int mm=n%4;
        int tt=n/4;
        for(int i=1;i<=tt-1;i++)
        {
            int t=(i-1)*4;
            cout<<t+2<<' '<<t+4<<' '<<t+1<<' '<<t+3<<' ';
        }
        if(mm==0)
            cout<<(tt-1)*4+3<<' '<<(tt-1)*4+1<<' '<<(tt-1)*4+4<<' '<<(tt-1)*4+2<<endl;
        if(mm==1)
            cout<<(tt-1)*4+1<<' '<<(tt-1)*4+4<<' '<<(tt-1)*4+2<<' '<<(tt-1)*4+5<<' '<<(tt-1)*4+3<<endl;
        if(mm==2)
            cout<<(tt-1)*4+1<<' '<<(tt-1)*4+4<<' '<<(tt-1)*4+2<<' '<<(tt-1)*4+5<<' '<<(tt-1)*4+3<<' '<<(tt-1)*4+6<<endl;
        if(mm==3)
            cout<<(tt-1)*4+1<<' '<<(tt-1)*4+4<<' '<<(tt-1)*4+2<<' '<<(tt-1)*4+6<<' '<<(tt-1)*4+3<<' '<<(tt-1)*4+5<<' '<<(tt-1)*4+7<<endl;

    }

}
原创文章 144 获赞 13 访问量 8664

猜你喜欢

转载自blog.csdn.net/Joker_He/article/details/106036030