C - A decorative fence POJ - 1037 (动态规划)

C - A decorative fence

 POJ - 1037 

Richard just finished building his new house. Now the only thing the house misses is a cute little wooden fence. He had no idea how to make a wooden fence, so he decided to order one. Somehow he got his hands on the ACME Fence Catalogue 2002, the ultimate resource on cute little wooden fences. After reading its preface he already knew, what makes a little wooden fence cute. 
A wooden fence consists of N wooden planks, placed vertically in a row next to each other. A fence looks cute if and only if the following conditions are met: 
�The planks have different lengths, namely 1, 2, . . . , N plank length units. 
�Each plank with two neighbors is either larger than each of its neighbors or smaller than each of them. (Note that this makes the top of the fence alternately rise and fall.) 
It follows, that we may uniquely describe each cute fence with N planks as a permutation a1, . . . , aN of the numbers 1, . . . ,N such that (any i; 1 < i < N) (ai − ai−1)*(ai − ai+1) > 0 and vice versa, each such permutation describes a cute fence. 
It is obvious, that there are many dierent cute wooden fences made of N planks. To bring some order into their catalogue, the sales manager of ACME decided to order them in the following way: Fence A (represented by the permutation a1, . . . , aN) is in the catalogue before fence B (represented by b1, . . . , bN) if and only if there exists such i, that (any j < i) aj = bj and (ai < bi). (Also to decide, which of the two fences is earlier in the catalogue, take their corresponding permutations, find the first place on which they differ and compare the values on this place.) All the cute fences with N planks are numbered (starting from 1) in the order they appear in the catalogue. This number is called their catalogue number. 


After carefully examining all the cute little wooden fences, Richard decided to order some of them. For each of them he noted the number of its planks and its catalogue number. Later, as he met his friends, he wanted to show them the fences he ordered, but he lost the catalogue somewhere. The only thing he has got are his notes. Please help him find out, how will his fences look like.

Input

The first line of the input file contains the number K (1 <= K <= 100) of input data sets. K lines follow, each of them describes one input data set. 
Each of the following K lines contains two integers N and C (1 <= N <= 20), separated by a space. N is the number of planks in the fence, C is the catalogue number of the fence. 
You may assume, that the total number of cute little wooden fences with 20 planks fits into a 64-bit signed integer variable (long long in C/C++, int64 in FreePascal). You may also assume that the input is correct, in particular that C is at least 1 and it doesn抰 exceed the number of cute fences with N planks.

Output

For each input data set output one line, describing the C-th fence with N planks in the catalogue. More precisely, if the fence is described by the permutation a1, . . . , aN, then the corresponding line of the output file should contain the numbers ai (in the correct order), separated by single spaces.

Sample Input

2
2 1
3 3

Sample Output

1 2
2 3 1

题意:给定木棒的数量n以及按典序排序的序号c,即满足每个木棒都比旁边两个木棒都低或都高(两端例外),求从左往右,从低到高的第c个序列是多少

思路:

思路:dp[i][j][0]表示用i根木棒,以第j大的作为第一根,且第一根比第二根短的方案数,dp[i][j][1]表示用i根木棒,以第j大的作为第一根,且第一根比第二根长的方案数

至于如何计数,举个简单的例子。

如1,2,3,4的全排列,共有4!种,求第10个的排列是(从1计 起) 

 先试首位是1,后234有3!=6种<10,说明首位1偏小,问题转换成 求2开头的第(10-6=4)个排列,而3!=6 >= 4,说明首位恰是2。 

第二位先试1(1没用过),后面2!=2个<4,1偏小,换成3(2用过 了)为第二位,待求序号也再减去2!,剩下2了。而此时2!>=2, 说明第二位恰好是3。 

第三位先试1,但后面1!<2,因此改用4。末位则是1了。  这样得出,第10个排列是2-3-4-1
思路参考https://blog.csdn.net/say_c_box/article/details/60322057 

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=110;
const int nmax = 23;
const double esp = 1e-9;
const double PI=3.1415926;
long long dp[nmax][nmax][2];
int vis[nmax],a[nmax];
void init(int n)
{
    memset(dp,0,sizeof(dp));
    dp[1][1][0]=1;  //初始条件
    dp[1][1][1]=1;
    for(int i=2; i<=n; i++)
    {
        for(int j=1; j<=i; j++)
        {
            for(int k=j; k<i; k++)
            {
                dp[i][j][0]+=dp[i-1][k][1];
            }
            for(int k=1; k<j; k++)
            {
                dp[i][j][1]+=dp[i-1][k][0];
            }
        }
    }
}
int main()
{
    int t;
    int n;
    ll id;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld",&n,&id);
        memset(vis,0,sizeof(vis));
        memset(a,0,sizeof(a));
        init(n);
        ll sum=0;
        for(int i=1; i<=n; i++)
        {
            int num=0;
            for(int j=1; j<=n; j++)
            {
                if(vis[j])
                    continue;
                ll ans=0;
                ++num;
                if(i==1)
                {
                    ans=dp[n][num][0]+dp[n][num][1];
                }
                else
                {
                    if(j>a[i-1]&&(i==2||a[i-2]>a[i-1])){  //从第i个向前看是高低高……型
                        ans+=dp[n-i+1][num][1];
                    }
                    else if(j<a[i-1]&&(i==2||a[i-2]<a[i-1])){//从第i个向前看是低高高……型
                        ans+=dp[n-i+1][num][0];
                    }
                }
                sum+=ans;
                if(sum>=id)
                {
                    sum-=ans;
                    a[i]=j;
                    vis[j]=1;
                    break;
                }
            }
        }
        for(int i=1; i<n; i++)
            printf("%d ",a[i]);
        printf("%d\n",a[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/83352394