POJ 1037 A decorative fence

A decorative fence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5280 Accepted: 1880

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


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 21
long long dp[2][MAXN][MAXN];
int N;
long long C;

void init(){
	int i, j, k;
	memset(dp, 0, sizeof(dp));
	dp[0][1][1] = dp[1][1][1] = 1;
	for (i = 2; i <= 20; i++){
		for (j = 1; j <= i; j++){
			for (k = j; k < i; k++){
				dp[0][i][j] += dp[1][i - 1][k];
			}
			for (k = 1; k < j; k++){
				dp[1][i][j] += dp[0][i - 1][k];
			}
		}
	}
}

int main(){
	int i, j, k, pre, T;
	int v[21];
	init();
	scanf("%d", &T);
	while(T--){
		scanf("%d%lld", &N, &C);
		for (i = 1; i <= N; i++) v[i] = i;
		i = N;
		for (j = 1; j <= i; j++){
//			printf("dp[%d][%d]=(%lld, %lld)\n", i, j, dp[0][i][j], dp[1][i][j]);
			if (dp[0][i][j] + dp[1][i][j] >= C) break;
			C -= dp[0][i][j] + dp[1][i][j];
		}
		if (C > dp[1][i][j]){
			C -= dp[1][i][j];
			k = 0;
		}else k = 1;
		printf("%d", v[j]);
		if (i != 1) printf(" ");
//		printf("v = %d\n", v[j]);
		pre = j;
		for (; j < i; j++) v[j] = v[j + 1];

		for (i = N - 1; i >= 1; i--){
			k ^= 1;
			if (k == 1){
				for (j = pre; j <= i; j++){
//					printf("dp[%d][%d][%d] = %lld\n", k, i, j, dp[k][i][j]);
					if (dp[k][i][j] >= C) break;
					C -= dp[k][i][j];
				}
			}else{
				for (j = 1; j < pre; j++){
//					printf("dp[%d][%d][%d] = %lld\n", k, i, j, dp[k][i][j]);
					if (dp[k][i][j] >= C) break;
					C -= dp[k][i][j];
				}			
			}
			printf("%d", v[j]);
			if (i != 1) printf(" ");
//			printf("v = %d\n", v[j]);
			pre = j;
			for (; j < i; j++) v[j] = v[j + 1];
		}
		printf("\n");
	}
	return 0;
}

/*
集合dp转普通dp, 此题中集合中数之间都是相对大小的关系,所以若同时增增减减集合中的数值,不会有影响
dp[0][i][j] 表示i个fence,以j开头,M型
dp[1][i][j] 表示i个fence,以j开头,W型
转移若是
dp[0][i][j] = sum(dp[1][i - 1][k], k = j + 1...N)
dp[1][i][j] = sum(dp[0][i - 1][k], k = 1...j - 1)
则会有fence重复使用
为了去重,定义前i个fence中j只能为1...i
考虑加入一个新开头j'
若j' == i + 1, 那么不会有重
若j' < i + 1, 必然会与之前加入的重复,考虑把大于等于j'的值都加1,
则所有数的范围还是1...i+1, 不会有重,且相对高低关系不变
所以j' < i + 1的,加进去也不会有重
dp[0][i][j] = sum(dp[1][i - 1][k], k = j...i)
注意,由于高度可能加1,所以k可以等于j
dp[1][i][j] = sum(dp[0][i - 1][k], k = 1...j - 1)

...这么想有些纠结...
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值