POJ -1037-A decorative fence(动归 +计数)

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7637 Accepted: 2905

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

  • 解题思路:刚看这道题时毫无思路,直到看了题解才懂了一点点。
  • 第一步:确定状态,这道题的状态有点复杂,如果我们把dp[i][j]定义为共有i根木棒以第j短开头的围成木栏的数量,那么dp[i][j]+= dp[i][j - 1] + ?,显然这个状态不成立, 那么让我们在细分一下,left[i][j] 定义为共有i根木棒以第j短为第一根并且第一根比第二根长的围成木栏的数量,right[i][j]定义为共有i根木棒以第j短为第一根并且第一根比第二根短的围成木栏的数量
    left[i][j] = right[i - 1][1 -> j - 1]
    right[i][j] = left[i - 1][j - > i - 1]
    总的方案是 left[i][j] + right[i][j];

    如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了。
    • 这样得出,第10个排列是2-3-4-1。

    本题待求方案的序号为C

    • 本题就是先假设第1短的木棒作为第一根,看此时的方案数P(1)是否>=C,如果否,则应该用第二短的作为第一根, C 减去P(1),再看此时方案数P(2)和C比如何。如果还 < C ,则应以第三短的作为第一根,C再减去P(2) ….若发现 第 i短的作为第一根时,方案数已经不小于C,则确定应该以第i短的作为第一根, C减去第 i短的作为第一根的所有方案数,然后再去确定第二根….
    • 微调:以第i短的木棒作第k根时,有UP和DOWN两类方案,先用DOWN的方案数和C比较
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL right[25][25];
LL left[25][25];
int road[25];//保存第c种的木板的顺序
bool used[25];//已经用过的木板
int main()
{
    memset(right, 0, sizeof(right));
    memset(left, 0, sizeof(left));
    left[1][1] = right[1][1] = 1; //动归初始化
    for (int i = 2; i <= 20; i++) {
        for (int j = 1; j <= i; j++) {
            for (int k = 1; k < j; k++) {
                left[i][j] += right[i - 1][k];
            }
            for (int k = j; k < i; k++) {
                right[i][j] += left[i - 1][k];
            }
        }
    }
    int t;
    int n;
    LL c;
    scanf("%d", &t);
    while (t--) {
        memset(road, 0, sizeof(road));
        memset(used, false, sizeof(used));
        scanf("%d%lld", &n, &c);
        int num = 0; 
        LL skip = 0, skipS; //skip 已经跳过的方案数,skipS先保存已经跳过的方案数
        for (int i = 1; i <= n; i++) {
            num = 0;
            for (int j = 1; j <= n; j++) {
                skipS = skip;
                if (!used[j]) {
                    num++;//剩下的木板的第num短
                    if (i == 1) {
                        skip += left[n][num] + right[n][num];
                    }
                    else {
                        if (j > road[i - 1] && (i == 2 || road[i - 2] > road[i - 1])) { //这样放置是正确的
                            skip += left[n - i + 1][num];
                        }   
                        if (j < road[i - 1] && (i == 2 || road[i - 2] < road[i - 1])) {//这样放置是正确的
                            skip += right[n - i + 1][num];
                        }
                    }
                }
                if (skip >= c) { //如果方案数 > c 说明第i根是j
                    road[i] = j;
                    printf("%d ", j);
                    used[j] = true;
                    skip = skipS;
                    break;
                } 
            }
        }
        printf("\n");
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值