Pku1037 A decorative fence 动态规划+递推

来源:互联网 发布:tc简单软件 编辑:程序博客网 时间:2024/06/05 18:04
A decorative fence
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3468 Accepted: 1122

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

22 13 3

Sample Output

1 22 3 1

Source

CEOI 2002
题目大意:要你求满足以下条件的,字典序为k,长度为n的数列:
这个数列由1~n组成,对于数列中的每个数Ai,满足(Ai-Ai-1)*(Ai-Ai+1)>0。
分析:这个问题类似于康托展开,可以用类似的方法解决。方法是,用f[i]表示以i为第一个数字的满足条件的数列有多少个,然后就能一步一步推出整个数列。具体方法如下:
定义f[i,j]表示以长度为i,以j为第一个数字,第二个数比第一个数字大的数列有多少个。g[i,j]的定义类似,不同的是第二个数字比第一个数字小而已。
这两个数组都可以用动态规划求,方程也不难写。
f[i,j]=∑g[i-1,k](j<=k<=n-1)
g[i,j]=∑f[i-1,k](1<=k<=j-1)
为什么呢?这个状态并没有考虑数列中出现了哪些数字,实际上也没有必要考虑。因为,对于一个数列,若第一个数字确定了,则后面的数字可以看作一个长度少1的全部数字加1的数列,因而得到了状态转移方程。
根据f和g数组,推导每位的数字的方法可以参看程序,应该不难理解。
注意的是,第一位要考虑比第二位大还是比第二位小,而后面每位只需根据上一次的决策来判断要比上一次大还是小就行了。
 
原创粉丝点击