C. Lucky Permutation----思维题

C. Lucky Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).

A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.

You have integer n. Find some lucky permutation p of size n.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size.

Output

Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.

Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation.

If there are multiple answers, you can print any of them.

Examples
input
1
output
1 
input
2
output
-1
input
4
output
2 4 1 3 
input
5
output
2 5 3 1 4 
题目链接:http://codeforces.com/contest/287/problem/C


题目的意思很简单,一个简单的置换,以数组的值为下标,可写成a[a[i]],然后求满足这种置换的一种情况

第一次碰到纯置换题,一个简单的思维题,我们可以多写几组,发现当n/2为奇数时我们必定输出-1,其实我们在写的时候就会发现,数字不可在本身的位置上,与其对应的数字(和为n+1的两个数)也不可以在这个位置上,所以说我们可以交换相邻的两组,如果n/2为奇数则会有一组无法交换,然后这个题就出来了。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int a[200000];
int main(){
    int n;
    scanf("%d",&n);
    if((n>>1)&1){
        return 0*printf("-1\n");
    }
    for(int i=1;i<=n;i++){
        a[i]=i;
    }
    for(int i=1;i<=n;i+=2){
        if(n&1&&i==n/2+1)
            i++;
        swap(a[i],a[i+1]);
    }
    //for(int i=1;i<=n;i++){
    //    printf("%d ",a[i]);
    //}
    //cout<<endl;
    for(int i=2;i<=n/2;i+=2){
        swap(a[i],a[n-i+1]);
    }
    for(int i=1;i<=n;i++){
        printf(i==1?"%d":" %d",a[i]);
    }
    cout<<endl;
    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值