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

 
题意:除了两端的木棒外,每一跟木棒,要么比它左右的两根都长,要么比它左右的两根都短
符合上述条件的栅栏建法有很多种,对于满足条件的所有栅栏, 按照字典序(从左到右, 从低到高) 排序。
 给定一个栅栏的排序号,请输出该栅栏, 即每一个木棒的长度.
 
题解:参考自pku_gw 代码 涨思路
C[i][k][DOWN] 是S(i)中以第k短的木棒打头的DOWN方案数,(第一根比第二根长成为down方案)
C[i][k][UP] 是S(i)中以第k短的木棒打头的UP方案数,第k短指i根中第k短  具体看代码
 
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
int UP=;
int DOWN=;
ll c[][][];
void init(int n)
{
memset(c,,sizeof(c));
c[][][UP]=c[][][DOWN]=;//初始为1种
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];//前i-1的down方案m>=k
for(int l=; l<=k-; l++)
c[i][k][DOWN]+=c[i-][l][UP];//前i-1的up方案 l<k
}
}
}
void fun(int n,ll cc)//排序计数处理 ,一位一位的判断 不断靠近cc
{
ll skipped=;//已经跳过的方案数
int seq[];
int used[];
memset(used,,sizeof(used));
for(int i=; i<=n; i++)
{
ll oldval=skipped;
int k;
int no=;
for(k=; k<=n; k++)
{
oldval=skipped;
if(!used[k])
{
no++;//k是剩下的木棒里第no短的
if(i==)//首位
skipped+=c[n][no][UP]+c[n][no][DOWN];
else
{ //剩下n-i+1条木棒 现在放置第no短的木棒k 判断k与已经确定的seq的前一条木棒的关系
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]=;
seq[i]=k;
skipped=oldval;
}
for(int i=; i<=n; i++)
if(i<n) printf("%d ",seq[i]);
else printf("%d\n",seq[i]); }
int main()
{
int T,s;
ll c;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d %I64d",&s,&c);
fun(s,c);
}
return ;
}

poj 1037 三维dp的更多相关文章

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

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

  2. 三维dp&codeforce 369_2_C

    三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...

  3. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  4. P1006 传纸条(二维、三维dp)

    P1006 传纸条 输入输出样例 输入 #1 复制 3 3 0 3 9 2 8 5 5 7 0 输出 #1 复制 34 说明/提示 [限制] 对于 30% 的数据,1≤m,n≤10: 对于 100% ...

  5. POJ 1037 DP

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

  6. poj上的dp专题

    更新中... http://poj.org/problem?id=1037 dp[i][j][0]表示序列长度为i,以j开始并且前两位下降的合法序列数目; dp[i][j][1]表示序列长度为i, 以 ...

  7. POJ 2096 (概率DP)

    题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...

  8. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  9. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

随机推荐

  1. LVS基于NAT模式搭建负载均衡群集

    LVS的基本架构图 负载均衡群集中,包括三个层次的组件: 1.第一层,负载调度器(BL) 前段至少有一个负载调度器(Load Balancer 或称为Director)负责响应并分发来自客户端的访问请 ...

  2. JQ常用方法(哈哈)

    1ajax请求 $(function(){   $("#send").click(function(){     $.ajax({     type:"get" ...

  3. Pandas 文本数据

    Pandas针对字符串配备的一套方法,使其易于对数组的每个元素(字符串)进行操作. 1.通过str访问,且自动排除丢失/ NA值 # 通过str访问,且自动排除丢失/ NA值 s = pd.Serie ...

  4. 012---Django的用户认证组件

    知识预览 用户认证 回到顶部 用户认证 auth模块 ? 1 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的 ...

  5. python搭建友盟以及个推推送web服务器

    一.友盟客户端demo: 由于SDK原因,新版Android Studio的Android API 28 Platform无法同步新建项目, 所以我最终选择下载android-studio-bundl ...

  6. [Link-Cut-Tree][BZOJ2631]Tree

    题面 Description: 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的 ...

  7. mysql学习第三天笔记

    连接连接是在多个表之间通过一定的连接条件,使表之间发生关联,进而能从多个表之间获取数据.在 WHERE子句中书写连接条件. 如果在多个表中出现相同的列名,则需要使用表名作为来自该表的列名的前缀. N个 ...

  8. Matplotlib库介绍

    pyplot的plot()函数 pyplot的中文显示 pyplot的文本显示 pyplot的子绘图区域

  9. Mysql 表转换成 Sqlite表

    目前的转换仅仅支持对没有外键的Mysql数据表 准备: 下载安装 Sqlite Expert 软件 一 获取Mysql中的.sql文件,获取过程省略可以直接导出sql文件 二 在Sqlite Expe ...

  10. Java-JNA使用心得2

    自5月初第一次尝试使用Java封装调用C的dll之后,已经先后经历了3次小项目了. 上月末是最近的一次项目实际,任务来的急时间又少,还好在加班加点后还是完成了任务,并把第二次没有实现的功能给实现了(C ...