题意

Language:Default
A decorative fence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8477 Accepted: 3244

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

Source

给定长度依次为1到n的木棒n个, 摆放规则为除了两边的木棒,剩下的木棒必须要比相邻的两个都高或者都低。求从小到大排列的第k个序列.

分析

参照superheroQAQ的题解。

其实在做过一些组合一类的题目之后这种题并不算太难 但是还是给了人很多启发并且有很多需要注意的地方.要想求第k个排列 f[i][j]表示由i根棍组成的j高度开头的种类数 这个是比较容易想到的 但是题目要求 波浪形!我记得noip考过一个简单的波浪形的题 于是再加一维状态 表示w型还是m形 求答案的时候就是dfs 这道题的dfs类似求排列 但有很多限制条件.

注意:

一根棍只能出现一次 并且我们要在意的是相对高度而不是实际高度 实际上就是说

dp[i][j][1/0]是可以由dp[i-1][j][0/1]转移过来的 举个例子:

dp[2][2][0] 2 1 ->dp[3][2][1] 2 3 1

时间复杂度\(O(n^2)\)

代码

#include<iostream>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll; co int N=21;
int n;
ll m,f[N][N][2];
void prework(){
f[1][1][0]=f[1][1][1]=1;
for(int i=2;i<N;++i)
for(int j=1;j<=i;++j){
for(int p=j;p<=i-1;++p) f[i][j][0]+=f[i-1][p][1];
for(int p=1;p<=j-1;++p) f[i][j][1]+=f[i-1][p][0];
}
}
bool used[N];
int main(){
prework();
for(int t=read<int>();t--;){
read(n),read(m);
memset(used,0,sizeof used);
int last,k;
for(int j=1;j<=n;++j){
if(f[n][j][1]>=m) {last=j,k=1;break;}
else m-=f[n][j][1];
if(f[n][j][0]>=m) {last=j,k=0;break;}
else m-=f[n][j][0];
}
used[last]=1;
printf("%d",last);
for(int i=2;i<=n;++i){
k^=1;
int j=0;
for(int len=1;len<=n;++len){
if(used[len]) continue;
++j;
if(k==0&&len<last||k==1&&len>last){
if(f[n-i+1][j][k]>=m) {last=len;break;}
else m-=f[n-i+1][j][k];
}
}
used[last]=1;
printf(" %d",last);
}
puts("");
}
return 0;
}

POJ1037 A decorative fence的更多相关文章

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

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

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

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

  3. A decorative fence

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

  4. 【POJ1037】A decorative fence(DP)

    BUPT2017 wintertraining(15) #6C 题意 给长度n的数列,1,2,..,n,按依次递增递减排序,求字典序第k小的排列. 题解 dp. up[i][j]表示长度为j,以第i小 ...

  5. poj1037 [CEOI 2002]A decorative fence 题解

    ---恢复内容开始--- 题意: t组数据,每组数据给出n个木棒,长度由1到n,除了两端的木棒外,每一根木棒,要么比它左右的两根都长,要么比它左右的两根都短.即要求构成的排列为波浪型.对符合要求的排列 ...

  6. poj 1037 A decorative fence

    题目链接:http://poj.org/problem?id=1037 Description Richard just finished building his new house. Now th ...

  7. OpenJ_Bailian - 1037 A decorative fence

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

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

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

  9. POJ1037A decorative fence(好dp)

    1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...

随机推荐

  1. 一些常见的“功能性”JS事件

    ————————倒序上升———————— (2)给一些 保存.提交 按钮,添加 防止重复提交 事件(常见一例) $("#submitRank").attr("disabl ...

  2. div上下切换(新增、删除、上下div切换)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. Qunar入职前自学笔记

    Q1. 什么是html,html发展历程 hyperText markup language 超文本标记语言 HTML是用于描述网页文档的标记语言.现在我们常常习惯于用数字来描述HTML的版本(如:H ...

  4. 递归与动态规划II-汉诺塔

    题目描述 有一个int数组arr其中只含有1.2和3,分别代表所有圆盘目前的状态,1代表左柱,2代表中柱,3代表右柱,arr[i]的值代表第i+1个圆盘的位置.比如,arr=[3,3,2,1],代表第 ...

  5. linux bash 命令

    export:显示所有的环境变量,如果你想获取某个变量的详细信息,使用 echo $VARIABLE_NAMEv whereis:使用系统自动构建的数据库来搜索可执行文件,源文件和手册页面 which ...

  6. 新手学习之浅析一下c/c++中的指针

    一.我们先来回忆一下指针的概念吧,方便下面的介绍 指针是存放地址值的变量或者常量.例如:int a=1;&a就表示指针常量(“&”表示取地址运算符,也即引用).int *b,b表示的是 ...

  7. Qt自定义控件系列(一) --- 圆形进度条

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...

  8. python 列表、元组

    列表 List(列表) 是 Python 中使用最频繁的数据类型. 列表可以完成大多数集合类的数据结构实现.它支持字符,数字,字符串甚至可以包含列表(即嵌套). 列表用 [ ] 标识,是 python ...

  9. webpack前端开发环境搭建

    要搭建webpack开发环境,首先要安装NodeJS,后面的过程均在NodeJS已经安装的基础上进行. 1. 首先建立一个工程目录,命名为,其目录结构如下: 其中dist目录用于存放生成的文件,src ...

  10. 十个有意思的Github Page

    1. Cooolis.github.io Cooolis是一个操作系统命令技巧备忘录 2. rfrd-tw.github.io 2018 台灣公投視覺化 3. confpad.github.io Co ...