- The King’s Ups and Downs

The king has guards of all different heights. Rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than the guards next to him or taller than the guards next to him (so the heights go up and down along the line). For example, seven guards of heights 160, 162, 164, 166, 168, 170 and 172 cm. could be arranged as: 


or perhaps: 


The king wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. To be able to do this, he needs to know for a given number of guards, n, how many different up and down orders there are: 

For example, if there are four guards: 1, 2, 3,4 can be arrange as: 

1324, 2143, 3142, 2314, 3412, 4231, 4132, 2413, 3241, 1423 

For this problem, you will write a program that takes as input a positive integer n, the number of guards and returns the number of up and down orders for n guards of differing heights. 

InputThe first line of input contains a single integer P, (1 <= P <= 1000), which is the number of data sets that follow. Each data set consists of single line of input containing two integers. The first integer, D is the data set number. The second integer, n (1 <= n <= 20), is the number of guards of differing heights. 
OutputFor each data set there is one line of output. It contains the data set number (D) followed by a single space, followed by the number of up and down orders for the n guards. 
Sample Input

4
1 1
2 3
3 4
4 20

Sample Output

. 1. 1 
2. 4 
. 3 10 
. 4 740,742,376,475,050 
Title effect: a set of data in accordance with the "low level of low level .." or "low height bottom ..." arrangement, the size of the data representative of the level,
the idea: the subject is very rare ah! ! ! N n assuming that the data in the middle of the data is read from the middle to the left past is high (n) a low level of high and low ..... . Read past the middle to the right is high (n) from the low level .... Therefore, the total number of permutations is multiplied by the number of permutations on the left of the right number of permutations, we first fixed the maximum number, then the number of n-1 extract the number j of n on the left
according to the arrangement is a combination of C (n-1, j) ; dp [j] [0] * dp [n-1-1] [1] * C (n-1, j ) wherein dp [j] [0] J n is the total number of aligned left personal dp [n-1-j] [1] is a right side n n-1-j the total number of aligned individual. Then consider the position of the highest number in the last ,, even increase it.
AC Code:
#include <the iostream> 
#include <algorithm>
 the using  namespace STD; 
typedef Long  Long LL; 
LL SUM [ 21 is ]; 
LL DP [ 21 is ] [ 2 ]; 
LL C [ 21 is ] [ 21 is ];
 void cinin () {// this function is calculated for the arrangement of the combined data is C (I, J) 
for ( int I = 0 ; I <= 21 is ; I ++ ) { C [I] [ 0 ] = C [I] [I] = . 1 ; } C [ . 1 ] [ . 1 ] = . 1 ; for ( int I = 2 ; I <= 21 is ; I ++ ) { for ( int J = . 1 ; J <I; J ++ ) { C [I] [J] = C [I- . 1 ] [J- . 1 ] + C [I- 1 ] [J]; } } } void Solve () { DP [ 0 ] [ 0 ] = 1 ; // 0 when left to 1 (0 is not, as do the lower multiplication) DP [ 1 ] [ 0 ] = 1 ; // number 1 is a left DP [ 1 ] [ 1 ] = 1; // the right in the same DP [ 0 ] [ . 1 ] = . 1 ; for ( int I = 2 ; I <= 21 ; I ++ ) // play table, a total of 20 to 21 ,, personal suffice here. { LL S = 0 ; for ( int J = 0 ; J <I; J ++ ) {// data can be regarded as the highest release position S + DP = [J] [ 0 ] * DP [IJ- . 1 ] [ . 1 ] * C [I- . 1 ] [J]; } SUM [I] = S; DP [I] [ 0 ] DP = [I] [ . 1] = S / 2 ; i // the individual on the left and on the right of the individual to be i and the sum is equal to s (I do not understand, later supplemented bar) } return ; } int main () { cinin ( ); SUM [ 0 ] = 0 ; SUM [ . 1 ] = . 1 ; Solve (); int T; CIN >> T; the while (T-- ) { int A, B; CIN >> A >> B; the printf ( " % D% LLD \ n- " , A, SUM [B]); } return 0 ; }

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11272214.html