• Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造


    B. Kyoya and Permutation

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/553/problem/B

    Description

    Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.

    Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.

    Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).

    Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].

    Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.

    Input

    The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).

    Output

    Print n space-separated integers, representing the permutation that is the answer for the question.

    Sample Input

    4 3

    Sample Output

    1 3 2 4

    HINT

    题意

    cycle,就是不在同一个位置的数,会成为一个圈

    每个cycle从大到小排序,所有cycle按第一个元素从大到小排序

    排序完了以后跟原来的序列不变就是合法

    然后让你输出第k大的合法数组

    题解:

    dp[i] = dp[i - 1] + dp[i - 2]

    比如1号位,可以放1

    1号位,也可以放2,如果放2

    那么考虑2号位

    显然不能比2大

    那就只能放1了

    1号位放3以上的都不行

    知道合法种类数有第n个fib数个数之后,构造方法就很显然了

    代码

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 2000001
    #define mod 1000000007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    ll c[100]={1,1,},k;
    int n,a[100];
    main()
    {
        for(int i=2;i<100;i++)
            c[i]=c[i-1]+c[i-2];
        cin>>n>>k;
        for(int i=1;i<=n;)
        {
            if (k>c[n-i])
            {
                k-=c[n-i];
                a[i]=i+1;
                a[i+1]=i;i+=2;
            }
            else
            {
                a[i]=i;
                i++;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
  • 相关阅读:
    docker微服务部署之:三,搭建Zuul微服务项目
    docker微服务部署之:二、搭建文章微服务项目
    docker微服务部署之:一,搭建Eureka微服务项目
    docker安装Tomcat软件,部署项目
    tomcat运行springboot项目war包
    使用Docker构建jdk1.8镜像
    docker安装MySQL软件
    InvalidKeyException: Illegal key size
    BeanUtils.copyProperties实现po,vo,dto之间的转换
    java集合之HashMap源码解读
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4599078.html
Copyright © 2020-2023  润新知