zoukankan      html  css  js  c++  java
  • 1976 Queen数列

    1976 Queen数列

     

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 黄金 Gold
     
     
    题目描述 Description

    将1到N的整数数列(1,2,3,……,N)打乱,变为数列a(a[1],a[2],a[3],……,a[N])。如果这个数列对于任意的i∈{1,2,3,……,N}都满足a[a[i]]=N+1-i,则这个数列叫做长度为N的Queen数列。

    现给你长度N,请输出字典序最小的Queen数列。

    所谓字典序最小,即为a[1]最小,在此基础上a[2]最小,在此基础上a[3]最小……

    输入描述 Input Description

    共一行,为一个整数N。

    输出描述 Output Description

    共一行,有i个整数,以空格隔开(行尾没有空格),第i个整数为a[i]。其中a为字典序最小的长度为N的Queen数列。如果不存在这样的数列,请输出一个0。

    样例输入 Sample Input

    Input1:

    3

    Input2:

    4

    Input3:

    5

    样例输出 Sample Output

    Output1:

    0

    Output2:

    2 4 1 3

    Output3:

    2 5 3 1 4

    数据范围及提示 Data Size & Hint

    不存在长度为3的Queen数列。

    2 4 1 3为字典序最小的长度为4的Queen数列。

    2 5 3 1 4为字典序最小的长度为5的Queen数列。

    对于20%的数据,N≤10;对于50%的数据,N≤1000;对于100%的数据,1≤N≤200000。

    分类标签 Tags 

    这规律找的,我也是醉了。

    代码:

    #include<cstdio>
    #include<iostream>
    using namespace std;
    int a[200010],n;
    int main(){
        scanf("%d",&n);
        if(n%4>1) {printf("0
    ");return 0;}
        for(int i=1;i<=n/4;++i){
            a[2*i-1]=2*i;
            a[2*i]=n+2-2*i;
            a[n-2*i+2]=n+1-2*i;
            a[n-2*i+1]=2*i-1;
        }
        if(n%2==1) a[n/2+1]=(n+1)/2;
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
        return 0;
    }
     
  • 相关阅读:
    HDU 1269 迷宫城堡
    HDU 4771 Stealing Harry Potter's Precious
    HDU 4772 Zhuge Liang's Password
    HDU 1690 Bus System
    HDU 2112 HDU Today
    HDU 1385 Minimum Transport Cost
    HDU 1596 find the safest road
    HDU 2680 Choose the best route
    HDU 2066 一个人的旅行
    AssetBundle管理机制(下)
  • 原文地址:https://www.cnblogs.com/shenben/p/5539725.html
Copyright © 2011-2022 走看看