A decorative fence
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 6357
Accepted: 2313
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 Z喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcmRlciBzb21lIG9mIHRoZW0uIEZvciBlYWNoIG9mIHRoZW0gaGUgbm90ZWQgdGhlIG51bWJlciBvZiBpdHMgcGxhbmtzIGFuZCBpdHMgY2F0YWxvZ3VlIG51bWJlci4gTGF0ZXIsIGFzIGhlIG1ldCBoaXMgZnJpZW5kcywgaGUgd2FudGVkIHRvIHNob3cgdGhlbSB0aGUgZmVuY2VzIGhlIG9yZGVyZWQsIGJ1dAogaGUgbG9zdCB0aGUgY2F0YWxvZ3VlIHNvbWV3aGVyZS4gVGhlIG9ubHkgdGhpbmcgaGUgaGFzIGdvdCBhcmUgaGlzIG5vdGVzLiBQbGVhc2UgaGVscCBoaW0gZmluZCBvdXQsIGhvdyB3aWxsIGhpcyBmZW5jZXMgbG9vayBsaWtlLgo8cCBjbGFzcz0="pst">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組成波浪形按字典序的第m種序列。
思路:
首先是dp,dp[i][j][k] - 總共 i 個數以相對位置為 j 開頭的序列上升(1)或下降(0)的序列個數。
dp[i][j][0]=∑dp[i-1][k][1] 1<=k
dp[i][j][1]=∑dp[i-1][k][0] j<=k
dp預處理之後就是從高位依次枚舉確定每個位數是幾了。
從前到後枚舉求得每一位:
枚舉一位時,計算在這樣的前綴下,後面的總的排列數。如果嚴格小於總編號,則該位偏小,換更大的數,同時更新總編號;若大於等於,則該位恰好,枚舉下一位,總編號不用更新。
先拋開此題,用最簡單的全排列問題來說明這種方法。
如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計起)
於是這題也能按照這種方法求解了,注意本題的序列是波浪形。
代碼:
#include
#include
#include
#include
#include
#include
#include