题目链接:http://poj.org/problem?id=1037

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 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 这是一个典型的递归问题,学习动规是看北大的资料看懂的,搞了半天才搞定。
C[i][k][DOWN] 是S(i)中以第k短的木棒打头的DOWN方
案数,C[i][k][UP] 是S(i)中以第k短的木棒打头的UP方案数,第k短指i根中第k短
 #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int UP =; const int DOWN =;
const int MAXN = ;
long long C[MAXN][MAXN][]; //C[i][k][DOWN] 是S(i)中以第k短的木棒打头的DOWN方案数,C[i][k][UP] 是S(i)中以第k短的木棒打头的UP方案数,第k短指i根中第k短
void Init(int n) {
memset(C,,sizeof(C));
C[][][UP] = C[][][DOWN] = ;
for( int i = ;i <= n; ++ i )
for( int k = ; k <= i; ++ k ) { //枚举第一根木棒的长度
for( int M = k; M <i ; ++M ) //枚举第二根木棒的长度
C[i][k][UP] += C[i-][M][DOWN];
for( int N = ; N <= k-; ++N ) //枚举第二根木棒的长度
C[i][k][DOWN] += C[i-][N][UP];
}
//总方案数是 Sum{ C[n][k][DOWN] + C[n][k][UP] } k = 1.. n;
}
void Print(int n, long long cc)
{
long long skipped = ; //已经跳过的方案数
int seq[MAXN]; //最终要输出的答案
int used[MAXN]; //木棒是否用过
memset(used,,sizeof(used));
for( int i = ; i<= n; ++ i ) { //依次确定每一个位置i的木棒序号
long long oldVal = skipped;
int k;
int No = ; //k是剩下的木棒里的第No短的,No从1开始算
for( k = ; k <= n; ++k ) { //枚举位置i的木棒 ,其长度为k
oldVal = skipped;
if( !used[k]) {
++ No; //k是剩下的木棒里的第No短的
if( i == )
skipped += C[n][No][UP] + C[n][No][DOWN];
else {
if( k > seq[i-] && ( i <= || seq[i-]>seq[i-]))
//合法放置
skipped += C[n-i+][No][DOWN];
else if( k < seq[i-] &&
(i<= || seq[i-]<seq[i-])) //合法放置
skipped += C[n-i+][No][UP];
}
if( skipped >= cc )
break;
}
}
used[k] = true;
seq[i] = k;
skipped = oldVal;
}
for( int i = ;i <= n; ++i )
if( i < n) printf("%d ",seq[i]);
else printf("%d",seq[i]);
printf("\n");
}
int main()
{
int T,n; long long c;
Init();
scanf("%d",&T);
while(T--) {
scanf("%d %lld",&n,&c);
Print(n,c);
}
return ;
}
												

poj 1037 A decorative fence的更多相关文章

  1. OpenJ_Bailian - 1037 A decorative fence

    Discription Richard just finished building his new house. Now the only thing the house misses is a c ...

  2. POJ1037 A decorative fence

    题意 Language:Default A decorative fence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 84 ...

  3. POJ1037 A decorative fence 【动态规划】

    A decorative fence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6489   Accepted: 236 ...

  4. poj 1037 三维dp

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

  5. A decorative fence

    A decorative fence 在\(1\sim n\)的全排列\(\{a_i\}\)中,只有大小交错的(即任意一个位置i满足\(a_{i-1}<a_i>a_{i+1}ora_{i- ...

  6. POJ1037A decorative fence(动态规划+排序计数+好题)

    http://poj.org/problem?id=1037 题意:输入木棒的个数n,其中每个木棒长度等于对应的编号,把木棒按照波浪形排序,然后输出第c个; 分析:总数为i跟木棒中第k短的木棒 就等于 ...

  7. POJ 1037 DP

    题目链接: http://poj.org/problem?id=1037 分析: 很有分量的一道DP题!!! (参考于:http://blog.csdn.net/sj13051180/article/ ...

  8. $Poj1037\ A\ Decorative\ Fence$ 计数类$DP$

    Poj  AcWing Description Sol 这题很数位$DP$啊, 预处理$+$试填法 $F[i][j][k]$表示用$i$块长度不同的木板,当前木板(第$i$块)在这$i$块木板中从小到 ...

  9. POJ 1037 (计数 + DP) 一个美妙的栅栏

    这道题总算勉勉强强看懂了,DP和计数都很不好想 DP部分: 称i根木棒的合法方案集合为S(i),第二根木棒比第一根长的方案称作UP方案,反之叫做DOWN方案 C[i][k][DOWN] 是S(i)中以 ...

随机推荐

  1. [SDOI2008]仪仗队

    P2158 [SDOI2008]仪仗队 题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线 ...

  2. ActionBar +Tab+ViewPager +Fragment 支持侧滑动完成办税工具的页面展示

    1:fragment_zhqrl.xml(征期日历) <?xml version="1.0" encoding="utf-8"?> <Line ...

  3. android特效

    http://houxiyang.com/archives/89/ http://blog.csdn.net/hjj0212/article/details/8535817 http://www.li ...

  4. STARTUP.A51详解及如何使能可重入函数

    $NOMOD51       ;Ax51宏汇编器控制命令:禁止预定义的8051;------------------------------------------------------------ ...

  5. CentOs6.5中安装和配置vsftp简明

    这篇文章主要介绍了CentOs6.5中安装和配置vsftp简明教程,需要的朋友可以参考下     一.vsftp安装篇 复制代码代码如下: # 安装vsftpdyum -y install vsftp ...

  6. Unit Test相关问题汇总

    1.测试私有方法(1)使用反射 public class Calcutate { public int test() { return add(2, 3); } private int add(int ...

  7. codility上的问题 (19)Sigma 2012

    题目: 像最大直方图一样给定一个数组是每个单位长度上的高度,求至少几个矩形可以拼出这个形状. 例如:给出的数组 H[0] = 8 H[1] = 8 H[2] = 5 H[3] = 7 H[4] = 9 ...

  8. .net常考面试题

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...

  9. Android Studio:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for this window 第一行代码

    学习<第一行代码>的时候,出现的错误. java.lang.RuntimeException: Unable to start receiver com.example.sevenun.l ...

  10. MySQL查询当前数据上一条和下一条的记录

    如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): ) [and other_cond ...