POJ 1037 A decorative fence(美丽栅栏)__动态规划

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

分析

题目大意:

N 个木棒, 长度分别为1, 2, …, N。构成美妙的栅栏。要求满足:除了两端的木棒外,每一跟木棒,要么比它左右的两根都长,要么比它左右的两根都短。即木棒呈现波浪状分布,这一根比上一根长了,那下一根就比这一根短,或反过来。符合上述条件的栅栏建法有很多种,对于满足条件的所有栅栏, 按照字典序(从左到右, 从低到高) 排序。问给定一个栅栏的排序号,请输出对应的栅栏方案, 即每一个木棒的长度。
输入木棒数N与方案数序号C,输出序号为C的方案中木棒的排列方式。

我们需要先用DP求出总方案数,再定位到序号C的方案。

求总方案数

课件中有状态转移方程:
称i根木棒的合法方案集合为S(i),则S(i)中木棒可排序可这样:
这里写图片描述
图1

这里写图片描述
图2

重点在于理解状态转移方程。 c[i][k][UP] 即是UP方案数,以k短的木棒 S(i)(L(k)) 打头,那么前一个木棒就得比 S(i)(L(k)) 短, S(i)(L(k)) 用掉时S(i)集合中少了一根木棒,所以剩下的木棒只能组成集合S(i-1),S(i)中比 S(i)(L(k)) 短的木棒 S(i)(L(k+1…i)) 变成了S(i-1)集合中的木棒 S(i-1)(L(k…i-1)) 。所以 c[i][k][UP] 即是所有符合条件的DOWN方案数的总和。

定位序号为C的方案的全排列位置

序号为C的方案的定位参考课件,排序记数:
如1, 2, 3, 4的全排列,共有4!种,求第10个的排列是(从1计起)?
- 先试首位是1,后面234有3! =6种<10,说明首位1偏小,问题转换成求2开头的第( 10-6=4)个排列,而3! =6 >= 4,说明首位恰是2。
- 第二位先试1( 1没用过),后面2! =2个<4,1偏小,换成3( 2用过了)为第二位,待求序号也再减去2!,剩下2了。而此时2!>=2,说明第二位恰好是3。
- 第三位先试1,但后面1! <2,因此改用4,待求序号减到1。末位则是1了。
这样得出,第10个排列是2-3-4-1。 复杂度不超过 n2。

实现

#include <iostream>
#include <cstring>
using namespace std;
#define UP 0
#define DOWN 1
#define MAXN 21
//栅栏中木棒数可达20个,全排列即有20!中可能,int不够放所以要用long long.
long long c[MAXN][MAXN][2];
int a[MAXN];
int used[MAXN];

void solve(int n, long long cc)
{
    long long skipped = 0;
    //确定位置i该放什么长度的木棒
    for (int i = 1; i <= n; i++) {
        int k = 0;
        for (int j = 1; j <= n; j++) {
            skipped = 0;
            if (!used[j]) {
                //是第k短的木棒
                k++;
                //位置1无法用前一项判断升降趋势,直接算总和。
                if (i == 1) {
                    skipped = c[n][k][UP] + c[n][k][DOWN];
                }
                else {
                    //找下降趋势木棒a形状只能是/\(i>2)或\(i=2),只找到i-1根木棒,所以a集合的中还有n-i+1根木棒。
                    if (a[i - 1] < j && (i <= 2 || a[i - 2] > a[i - 1])) {
                        skipped = c[n - i + 1][k][DOWN];
                    }
                    //找上升趋势方案数
                    else if (a[i - 1] > j && (i <= 2 || a[i - 2] < a[i - 1])) {
                        skipped = c[n - i + 1][k][UP];
                    }
                }
                if (skipped >= cc) {
                    used[j] = true;
                    a[i] = j;
                    break;
                }
                else {
                    //继续缩小范围,详细原因看"分析"节。
                    cc -= skipped;
                }
            }
        }
    }
}

//初始化c[i][k][UP]与c[i][k][DOWN].
void init(int max)
{
    memset(c, 0, sizeof(c));
    c[1][1][UP] = c[1][1][DOWN] = 1;
    for (int i = 2; i <= max; i++) {
        for (int k = 1; k <= max; k++) {
            //求c[i][k][UP]=∑c[i-1][m][DOWN], k<=m<=i-1
            for (int m = k; m < i; m++) {
                c[i][k][UP] += c[i - 1][m][DOWN];
            }
            //求c[i][k][DOWN]=c[i-1][n][UP], 1<=n<=k-1
            for (int n = 1; n < k; n++) {
                c[i][k][DOWN] += c[i - 1][n][UP];
            }
        }
    }
}

int main()
{
//  freopen("in.txt", "r", stdin);
    int cases;
    scanf("%d", &cases);
    init(MAXN - 1);
    while (cases--) {
        int n;
        long long cc;
        scanf("%d %lld", &n, &cc);
        memset(a, 0, sizeof(a));
        memset(used, 0, sizeof(used));
        solve(n, cc);
        for (int i = 1; i <= n; i++) {
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    return 0;
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值