POJ 1037 (DP)

A decorative fence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7423 Accepted: 2805

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

Source

CEOI 2002


题意:一个栅栏由N个长度1~N的木板组成,并且栅栏符合波浪状,即整体高度是由高低高低....或低高低高....组成,把所有可以组成的情况按字典序排序,求出第K个的情况。


     此题很难理解,听说是黑书上面的例题,用到了组合排列和DP,我参考了很多博客,花了很多天才看懂,然后自己按照自己的理解写出来,然后改进了一些步骤。

首先先讲一下排列:

参考:

一、求字典序的第i个排列

参见:  http://jay23jack.blog.163.com/blog/static/317951942009130215813/

 

      暴力枚举可以很轻松地得到符合的排列,事实上不妨ms写一个,来产生更强的样例。当然,答案不会是暴力,而是一种很常见的方法,可以说是这种求指定编号问题的通法吧!
      直接一位一位枚举答案!从前到后枚举求得每一位:枚举一位时,计算在这样的前缀下,后面的总的排列数。如果严格小于总编号,则该位偏小,换更大的数,同时更新总编号;若大于等于,则该位恰好,枚举下一位,总编号不用更新。 
      先抛开此题,用最简单的全排列问题来说明这种方法。如1,2,3,4的全排列,共有4!种,求第10个的排列是?
      先试首位是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。(从1计起)
      这种方法的核心在于求,给定前缀下后面总的排列数。全排列问题较易求。同时还需记录前面用过的数字。
      再回到此题,同是这样来求。但求后面总的排列数时,用到了递推,或者说dp,不难的方程。
      搞懂了后,去poj上做题,先是WA,试过书上的样例后,发现忘记判断“波浪形”了。于是慨叹,强大的数据是成功调试的有力保证!以后还是要不厌其烦地写一些样例来才行啊!

 

Sam:在求字典序的第i个排列时,关键是求得每种特定前缀子串的可能排列数目,如上面例子中,要求出字典序第i大,需要知道以下序列的个数 (*代表可以使任何可能数字):

      这道题按照上面的思路,我们首先应该求出来当长度len确定时num开头的组合有几种,这样就可以找出这个数是否是我们需要的。接下来就是求这个过程了,这个过程可以用DP来写。

      首先是确定状态,dp【i】【j】【K】,表示长度为i 时j 为第一个数,并且当K==0时第一个数和第二个数是呈递减的趋势,K==1时呈递增趋势时的组合情况,

dp【i】【j】【0】=sum(dp【i-1】【t】【1】)(1<=t<j)

dp【i】【j】【1】=sum(dp【i-1】【t】【0】)(j<=t<i)

这个状态转移方程是我一直想不通的地方,最后突然顿悟了。首先这个状态非常抽象,此 j 非彼 j,而是在i个数里面的高度第j低的数,很抽象吧,举个栗子,假如N=2,高度分别为4,3,dp【2】【1】【1】=sum(dp【1】【1】【0】)如何理解,首先是dp【2】【1】【1】这个状态表示在{4,3}的数列里首位是高度第1低的数3并且呈递增趋势,这个状态是由减去高度第1低的数3后的剩余数列{4}中第1低的数并且呈递减趋势的状态dp【1】【1】【0】推出来的。 

     所以,dp【i】【j】【h】这个状态里的i指的是剩余数列内高度第j低的数作为首位,并且呈递增或递减的趋势时的组合数。这个状态长度i对应的数列是都是不同的,但是符合DP的过程。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <vector>
#define inf 0x6fffffff
#define LL long long
#define mem(p,k) memset(p,k,sizeof(p));
using namespace std;
set<int> a;//因为数列是变化的,每次求得都是高度第N的值,所以用set来求出每个对应值。
set<int>::iterator it;
LL dp[25][25][2],n,c,h[25];
void dfs(int la,LL k,LL c,int x){
    if(k==0){
            return;
    }
    it=a.begin();
    if(x==0){
        for(int i=1,f;i<=a.size();i++,it++){
            f=*it;
            if(dp[k][i][1]>=c){
                h[k]=f;
                a.erase(it);
                return dfs(f,k-1,c,1);
            }
            else c-=dp[k][i][1];
          
        }
    }
    else{
        for(int i=1,f;i<=a.size();i++,it++){//因为不知道上次的j在哪里,只能从头遍历,把低于上一个数的值都忽略。
            f=*it;
            if(f<la)continue;
            if(dp[k][i][0]>=c){
                h[k]=f;
                a.erase(it);
                return dfs(f,k-1,c,0);

            }
            else c-=dp[k][i][0];
          
        }
    }

}
int main(){
    int k;
    cin>>k;
    dp[1][1][1]=dp[1][1][0]=1;
    for(int i=2;i<=20;i++){
            for(int j=1;j<=i;j++){
                for(int k=1;k<i;k++){
                    if(k<j){
                        dp[i][j][0]+=dp[i-1][k][1];
                    }
                    else  dp[i][j][1]+=dp[i-1][k][0];
                }
            }
    }
    while(k--){
        cin>>n>>c;
        a.clear();
        mem(h,0);
        for(int i=1;i<=n;i++)a.insert(i);//把所有的长度都插入SET
        for(int i=1;i<=n;i++){
            if(dp[n][i][0]+dp[n][i][1]>=c){
                h[n]=i;
                a.erase(i);
                if(dp[n][i][0]>=c){
                    dfs(i,n-1,c,0);
                }
                else dfs(i,n-1,c-dp[n][i][0],1);
                break;
            }
            else c-=dp[n][i][0]+dp[n][i][1];
        }
        for(int i=n;i>=1;i--){
                cout<<h[i];
                if(i!=1)cout<<" ";
                else cout<<endl;
        }
    }

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值