Poj1037 a decorative fence [Dynamic Planning]

Source: Internet
Author: User

A decorative fence
Time limit:1000 ms   Memory limit:10000 K
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 make a wooden fence, so he decided to order one. somehow he got his hands on the Acme fence catalogger 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 using 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 in C/C ++, int64 in freepascal ). you may also assume that the input is correct, in particle that C is at least 1 and it Doesn has 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 shoshould contain the numbers AI (in the correct order), separated by single spaces.

Sample Input

22 13 3

Sample output

1 22 3 1
It took a whole day to read the lecture notes, which is more difficult to understand than scanning lines .. this is the most difficult exercise I have encountered .. the number of sticks with a given length of 1 to n must be equal to or lower than that of the adjacent two sticks. Calculate the C sequence in ascending order. Solution: the state equation DP [I] [J] [0] is used to find the number of arrangement schemes in ascending order for the first two sticks to start with J sticks, DP [I] [J] [1] is used to find the number of arrangement schemes starting with the J wooden rod in the I wooden rod and the first two are in descending order, the detailed explanation is in the comments of the handout code.

#include <stdio.h>#include <string.h>#define maxn 22#define UP 0#define DOWN 1int vis[maxn], ans[maxn];__int64 dp[maxn][maxn][2];void Init(int n){int i, j, k;dp[1][1][UP] = dp[1][1][DOWN] = 1;for(i = 2; i <= n; ++i){ //i is the amount of total sticksfor(j = 1; j <= i; ++j){ //j is the first stick's positionfor(k = j; k < i; ++k) //k is the subSolution of dp arraydp[i][j][UP] += dp[i-1][k][DOWN];for(k = 1; k < j; ++k)dp[i][j][DOWN] += dp[i-1][k][UP];}}}void Print(int n, __int64 c){memset(vis, 0, sizeof(vis));int i, j, k, rank;__int64 skip = 0, pre = 0;for(i = 1; i <= n; ++i){ //i is the position to select nowrank = 0; //the rank's min stickfor(j = 1; j <= n; ++j){ //j is the stick to be selectpre = skip;if(!vis[j]){++rank;if(i == 1) skip += dp[n][rank][UP] + dp[n][rank][DOWN];else if(j > ans[i-1] && (i == 2 || ans[i-2] > ans[i-1]))skip += dp[n-i+1][rank][DOWN];else if(j < ans[i-1] && (i == 2 || ans[i-2] < ans[i-1]))skip += dp[n-i+1][rank][UP];if(skip >= c) break; }}ans[i] = j;vis[j] = 1;skip = pre;}for(i = 1; i <= n; ++i)if(i != n) printf("%d ", ans[i]);else printf("%d\n", ans[i]);}int main(){int t, n;__int64 c;Init(20);scanf("%d", &t);while(t--){scanf("%d%I64d", &n, &c);Print(n, c);}return 0;}


Handout code:
# Include <stdio. h> # include <string. h> # define up 0 # define down 1 # define maxn 25 long C [maxn] [maxn] [2]; // C [I] [k] [Down] is the number of down schemes for hitting the head of the K-short wooden rod in S (I, c [I] [k] [Up] // number of up schemes starting with the K-short wooden rod in S (I, the K short refers to the K short void Init (int n) {memset (C, 0, sizeof (c) in the I root )); c [1] [1] [Up] = C [1] [1] [Down] = 1; int I, K, M, N; for (I = 2; I <= N; ++ I) {for (k = 1; k <= I; ++ K) {// enumerate the length of the first wooden rod // enumerate the second length for (M = K; m <I; ++ m) c [I] [k] [Up] + = C [I-1] [M] [Down]; // enumerative second length for (n = 1; n <K; ++ N) c [I] [k] [Down] + = C [I-1] [N] [Up];} // The total number of solutions is sum {C [N] [k] [Down] + C [N] [k] [Up]} k = 1 .. n;} void print (int n, long CC) {long skipped = 0, oldval; // Number of skipped schemes int seq [maxn]; // The final answer int used [maxn]; // whether memset (used, 0, sizeof (used) is used for the wooden stick; For (INT I = 1, K; I <= N; ++ I) {// The sequence of the wood rod whose position I is determined. Int NO = 0; For (k = 1; k <= N; ++ K) {// enumeration position I's wooden stick oldval = skippe D; If (! Used [k]) {++ no; // K is the if (I = 1) shorter than the NO in the remaining sticks) skipped + = C [N] [No] [Up] + C [N] [No] [Down]; // find the valid location else if (k> seq [I-1] & (I = 2 | seq [I-2]> seq [I-1]) skipped + = C [n-I + 1] [No] [Down]; else if (k <seq [I-1] & (I = 2 | seq [I-2] <seq [I-1]) skipped + = C [n-I + 1] [No] [Up]; If (skipped> = cc) break;} used [k] = 1; SEQ [I] = K; skipped = oldval;} For (INT I = 1; I <= N; ++ I) if (I <n) printf ("% d", seq [I]); else printf ("% d \ n", seq [I]);} int main () {int t, n; long long C; Init (20); scanf ("% d", & T); While (t --) {scanf ("% d % LLD", & N, & C); print (N, C);} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.