poj 1037 三维dp

A decorative fence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7221   Accepted: 2723

Description

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 di erent 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

Source

 
题意:除了两端的木棒外,每一跟木棒,要么比它左右的两根都长,要么比它左右的两根都短
符合上述条件的栅栏建法有很多种,对于满足条件的所有栅栏, 按照字典序(从左到右, 从低到高) 排序。
 给定一个栅栏的排序号,请输出该栅栏, 即每一个木棒的长度.
 
题解:参考自pku_gw 代码 涨思路
C[i][k][DOWN] 是S(i)中以第k短的木棒打头的DOWN方案数,(第一根比第二根长成为down方案)
C[i][k][UP] 是S(i)中以第k短的木棒打头的UP方案数,第k短指i根中第k短  具体看代码
 
 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 //#include<bits/stdc++.h>
 8 #include<iostream>
 9 #include<cstring>
10 #include<cmath>
11 #include<cstdio>
12 #define ll long long
13 #define mod 1000000007
14 #define PI acos(-1.0)
15 using namespace std;
16 int UP=0;
17 int DOWN=1;
18 ll c[25][25][2];
19 void init(int n)
20 {
21     memset(c,0,sizeof(c));
22     c[1][1][UP]=c[1][1][DOWN]=1;//初始为1种
23     for(int i=2; i<=n; i++)
24     {
25         for(int k=1; k<=i; k++)
26         {
27             for(int m=k; m<i; m++)
28                 c[i][k][UP]+=c[i-1][m][DOWN];//前i-1的down方案m>=k
29             for(int l=1; l<=k-1; l++)
30                 c[i][k][DOWN]+=c[i-1][l][UP];//前i-1的up方案 l<k
31         }
32     }
33 }
34 void fun(int n,ll cc)//排序计数处理 ,一位一位的判断 不断靠近cc
35 {
36     ll skipped=0;//已经跳过的方案数
37     int seq[25];
38     int used[25];
39     memset(used,0,sizeof(used));
40     for(int i=1; i<=n; i++)
41     {
42         ll oldval=skipped;
43         int k;
44         int no=0;
45         for(k=1; k<=n; k++)
46         {
47             oldval=skipped;
48             if(!used[k])
49             {
50                 no++;//k是剩下的木棒里第no短的
51                 if(i==1)//首位
52                     skipped+=c[n][no][UP]+c[n][no][DOWN];
53                 else
54                 {  //剩下n-i+1条木棒 现在放置第no短的木棒k 判断k与已经确定的seq的前一条木棒的关系
55                     if(k>seq[i-1]&&(i<=2||seq[i-2]>seq[i-1]))
56                         skipped+=c[n-i+1][no][DOWN];//判断合理的放置
57                     else if(k<seq[i-1]&&(i<=2||seq[i-2]<seq[i-1]))
58                         skipped+=c[n-i+1][no][UP];
59                 }
60                 if(skipped>=cc)//当跳过的方案数大于询问的数目跳出
61                     break;
62             }
63         }
64         used[k]=1;
65         seq[i]=k;
66         skipped=oldval;
67     }
68     for(int i=1; i<=n; i++)
69         if(i<n) printf("%d ",seq[i]);
70         else    printf("%d
",seq[i]);
71 
72 }
73 int main()
74 {
75     int T,s;
76     ll c;
77     init(20);
78     scanf("%d",&T);
79     while(T--)
80     {
81         scanf("%d %I64d",&s,&c);
82         fun(s,c);
83     }
84     return 0;
85 }
原文地址:https://www.cnblogs.com/hsd-/p/5712908.html