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

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

⊙﹏⊙     这题做了整整一天,看着讲义做的,理解起来比扫描线还难..算是我眼下碰到的最难的动规题了..

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

题解:状态方程dp[i][j][0]用来求在i个木棒中以第j根木棒打头且前两根为升序的排列方案数,dp[i][j][1]用来求在i个木棒中以第j根木棒打头且前两根为降序的排列方案数。详解在讲义代码的凝视里。

#include <stdio.h>
#include <string.h>
#define maxn 22
#define UP 0
#define DOWN 1 int vis[maxn], ans[maxn];
__int64 dp[maxn][maxn][2]; void Init(int n)
{
int i, j, k;
dp[1][1][UP] = dp[1][1][DOWN] = 1;
for(i = 2; i <= n; ++i){ //i is the amount of total sticks
for(j = 1; j <= i; ++j){ //j is the first stick's position
for(k = j; k < i; ++k) //k is the subSolution of dp array
dp[i][j][UP] += dp[i-1][k][DOWN];
for(k = 1; k < j; ++k)
dp[i][j][DOWN] += dp[i-1][k][UP];
}
}
} void Print(int n, __int64 c)
{
memset(vis, 0, sizeof(vis));
int i, j, k, rank;
__int64 skip = 0, pre = 0;
for(i = 1; i <= n; ++i){ //i is the position to select now
rank = 0; //the rank's min stick
for(j = 1; j <= n; ++j){ //j is the stick to be select
pre = skip;
if(!vis[j]){
++rank;
if(i == 1) skip += dp[n][rank][UP] + dp[n][rank][DOWN];
else if(j > ans[i-1] && (i == 2 || ans[i-2] > ans[i-1]))
skip += dp[n-i+1][rank][DOWN];
else if(j < ans[i-1] && (i == 2 || ans[i-2] < ans[i-1]))
skip += dp[n-i+1][rank][UP];
if(skip >= c) break;
}
}
ans[i] = j;
vis[j] = 1;
skip = pre;
}
for(i = 1; i <= n; ++i)
if(i != n) printf("%d ", ans[i]);
else printf("%d\n", ans[i]);
} int main()
{
int t, n;
__int64 c;
Init(20);
scanf("%d", &t);
while(t--){
scanf("%d%I64d", &n, &c);
Print(n, c);
}
return 0;
}

附讲义代码:

#include <stdio.h>
#include <string.h>
#define UP 0
#define DOWN 1
#define maxn 25 long long C[maxn][maxn][2];
//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, 0, sizeof(C));
C[1][1][UP] = C[1][1][DOWN] = 1;
int i, k, M, N;
for(i = 2; i <= n; ++i){
for(k = 1; k <= i; ++k){ //枚举第一根木棒的长度
//枚举第二根长度
for(M = k; M < i; ++M) C[i][k][UP] += C[i-1][M][DOWN];
//枚举第二根长度
for(N = 1; N < k; ++N) C[i][k][DOWN] += C[i-1][N][UP];
}
}
//总方案数是 Sum{ C[n][k][DOWN] + C[n][k][UP] } k = 1.. n;
} void Print(int n, long long cc)
{
long long skipped = 0, oldVal; //已经跳过的方案数
int seq[maxn]; //终于要输出的答案
int used[maxn]; //木棒是否用过
memset(used, 0, sizeof(used));
for(int i = 1, k; i <= n; ++i){ //依次确定位置i的木棒序号
int No = 0;
for(k = 1; k <= n; ++k){ //枚举位置i的木棒
oldVal = skipped;
if(!used[k]){
++No; //k是剩下木棒里的第No短的
if(i == 1) skipped += C[n][No][UP] + C[n][No][DOWN];
//下面寻找合法位置
else if(k > seq[i-1] && (i == 2 || seq[i-2] > seq[i-1]))
skipped += C[n-i+1][No][DOWN];
else if(k < seq[i-1] && (i == 2 || seq[i-2] < seq[i-1]))
skipped += C[n-i+1][No][UP];
if(skipped >= cc) break;
}
}
used[k] = 1;
seq[i] = k;
skipped = oldVal;
}
for(int i = 1; i <= n; ++i)
if(i < n) printf("%d ", seq[i]);
else printf("%d\n", seq[i]);
} int main()
{
int T, n; long long c;
Init(20);
scanf("%d", &T);
while(T--){
scanf("%d%lld", &n, &c);
Print(n, c);
}
return 0;
}

POJ1037 A decorative fence 【动态规划】的更多相关文章

  1. POJ1037 A decorative fence

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

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

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

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

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

  4. A decorative fence

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

  5. 【POJ1037】A decorative fence(DP)

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

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

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

  7. poj 1037 A decorative fence

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

  8. OpenJ_Bailian - 1037 A decorative fence

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

  9. POJ1037A decorative fence(好dp)

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

随机推荐

  1. Selenium2+python自动化61-Chrome浏览器(chromedriver)【转载】

    前言 selenium2启动Chrome浏览器是需要安装驱动包的,但是不同的Chrome浏览器版本号,对应的驱动文件版本号又不一样,如果版本号不匹配,是没法启动起来的. 一.Chrome遇到问题 1. ...

  2. 【转】持续集成 Sonar 平台搭建及 Sonar 自定义规则打包部署篇

    引言 基于阿里开发手册的sonar自定义插件工程 开源地址: https://github.com/tigerge000/sonar-java-custom-rules.git由于最近来问童鞋,就算写 ...

  3. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

  4. POJ 1988 Cube stacking【并查集高级应用+妙用deep数组】

    Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...

  5. 模板-网络流-Dinic

    //Dinic struct Edge{ int from,to,cap,flow; Edge(){ } Edge(int a,int b,int c,int d){ from=a; to=b; ca ...

  6. 礼物(BFS)

    礼物 时间限制: 1 Sec  内存限制: 64 MB提交: 39  解决: 4[提交][状态][讨论版] 题目描述 给出一个n行m列的点阵,“.”表示可通行格子,“#”表示不可通行格子,“K”表示国 ...

  7. 安装redis扩展

    1.安装redis模块 wget https://codeload.github.com/phpredis/phpredis/zip/php7 unzip php7 cd phpredis-php7/ ...

  8. [Luogu P4198]楼房重建(线段树)

    题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些事件发生在一个 ...

  9. 《深入理解Spark-核心思想与源码分析》(二)第二章Spark设计理念和基本架构

    若夫乘天地之正,而御六气之辩解,以游无穷者,彼且恶乎待哉? ——<庄子.逍遥游> 翻译:至于遵循宇宙万物的规律,把握“六气”的变化,遨游于无穷无尽的境域,他还仰赖什么呢! 2.1 初始Sp ...

  10. maven-pom-profile

    出处: http://blog.csdn.net/taiyangdao/article/details/52390095