POJ1037:A decorative fence(DP)

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

题意:有1~n的n根木棒,波浪形全排列的第m个数列是什么

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define LL long long
#define N 25
#define MOD 19999997
#define INF 0x3f3f3f3f
#define EXP 1e-8

const double Pi = acos(-1.0);

int t,n;
LL m;
LL dp[N][N][2];//0-UP,1-DOWN
//dp[i][k][1] 是以第k短的木棒打头的DOWN方案数,C[i][k][0]是以第k短的木棒打头的UP方案数,第k短指i根中第k短
int a[N],ans[N],vis[N];

void solve()
{
    int i,j,k,cnt;
    LL now=0,pre;//已经枚举的方案数
    MEM(vis,0);
    MEM(ans,0);
    UP(i,1,n)
    {
        cnt = 0;
        UP(j,1,n)//枚举所有木棒
        {
            pre = now;
            if(!vis[j])
            {
                cnt++;
                if(i==1)
                    now+=dp[n][cnt][0]+dp[n][cnt][1];
                else if(j>ans[i-1] && (i==2 || ans[i-2]>ans[i-1]))
                    now+=dp[n-i+1][cnt][1];
                else if(j<ans[i-1] && (i==2 || ans[i-2]<ans[i-1]))
                    now+=dp[n-i+1][cnt][0];
                if(now>=m) break;//后面所有数的全排列的数量加上来超过m,我们能确定这位放j
            }
        }
        vis[j] = 1;
        ans[i]=j;
        now = pre;
    }
    printf("%d",ans[1]);
    UP(i,2,n)
    printf(" %d",ans[i]);
    printf("\n");
}

int main()
{
    int i,j,k;
    MEM(dp,0);
    dp[1][1][0]=dp[1][1][1] = 1;
    UP(i,2,20)
    {
        UP(j,1,i)//枚举第一根木棒的长度
        {
            //第二根的长度
            UP(k,j,i-1) dp[i][j][0]+=dp[i-1][k][1];
            UP(k,1,j-1) dp[i][j][1]+=dp[i-1][k][0];
        }
    }
    scanf("%d",&t);
    W(t--)
    {
        scanf("%d%I64d",&n,&m);
        solve();
    }

    return 0;
}
时间: 2024-11-08 18:19:53

POJ1037:A decorative fence(DP)的相关文章

POJ1037 A decorative fence 【动态规划】

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 m

HDU 1003:Max Sum(DP)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142742    Accepted Submission(s): 33225 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU4939:Stupid Tower Defense(DP)

Problem Description FSF is addicted to a stupid tower defense game. The goal of tower defense games is to try to stop enemies from crossing a map by building traps to slow them down and towers which shoot at them as they pass. The map is a line, whic

HDU4901:The Romantic Hero(DP)

Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can't refus

codeforces 277.5 div2 F:组合计数类dp

题目大意: 求一个 n*n的 (0,1)矩阵,每行每列都只有两个1 的方案数 且该矩阵的前m行已知 分析: 这个题跟牡丹江区域赛的D题有些类似,都是有关矩阵的行列的覆盖问题 牡丹江D是求概率,这个题是方案数,也比较相似.. 这种题中,因为只要求方案数..我们只要关注几行几列有几个1,而不必要关注具体的位置 题解: 行列都需要处理,因此考虑记录列的状态,然后一行一行的转移 最暴力的方程: dp[i][j][k][t] 表示已经确定了 i 行 有 j列已经有两个1,有k列只有一个1,有t列一个1也没

poj3254:基础状压dp

第二个状压dp 做过的第一个也是放牛问题,两头牛不能相邻 这个题多了一个限制,就是有些位置不能放牛 于是先与处理一下每一行所有不能放牛的状态,处理的过程直接对每一个不能放牛的状态或以下 ac代码: #include <iostream> #include <stdio.h> #include<string.h> #include<algorithm> #include<string> #include<ctype.h> using n

POJ 2955:Brackets(区间DP)

http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = dp[i+1][j-1] + 2,表示由上一个状态加上当前的贡献. 然后和普通的区间合并一样去更新. 1 #include <cstring> 2 #include <cstdio> 3 #include <iostream> 4 #include <string&

UVAlive 10154:Dire Wolf 区间DP

Dire Wolf 题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5073 题意: 有一排的狼,每只狼有一个基础攻击力a[i],和一个加成值b[i](可以给相邻的两只狼加b[i]点攻击力,这只狼死后就不加了),你每杀一只狼需要花费能量值(狼的当前攻击力),你杀了这只狼周围的狼会自动往中间聚集(即不会有空隙),求杀

HDU 2136:Computer(树形DP)

http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Computer Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to on