HDU 4489 the king's ups and downs (recursive + permutation and combination)

Source: Internet
Author: User
The King's ups and downs

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 184 accepted submission (s): 116

Problem descriptionthe 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
He (so the heights go up and down along the Line). For example, seven guards of heights 160,162,164,166,168,170 and 172. cocould be arranged:

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:

1324,214 3, 3142,231 4, 3412,423 1, 4132,241 3, 3241,142 3

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

41 12 33 44 20

Sample output

1 12 43 104 740742376475050

Sourcegreater New York 2012

RecommendliuyidingQuestion:We know there are N soldiers. Please arrange them in high and low heights .. Or low or low...How many troubleshooting methods are there for a team in this high-low and high-staggered manner.
Analysis:Keywords: RecursionKnown I-1 individual High and Low staggered layout, now more than one I, asked I to join the team is still satisfiedHigh/low crossover.DP [I] indicates the number of bars with high/low interleaved values of I.
How do I join the team?Classification-1. He can directly stand on both sides of the team2. He can insert it to the team.The first case: Standing on both sides of the team can be directly joined, so the number of solutions is equal to the number of individuals in the I-1. Case 2: how to insert a queue? -- The two sides of the insert must be satisfied. The left side ends with the peak and the right side begins with the peak. Or end with a valley on the left and start with a valley on the right.In this way, we choose J from the I-1 and stand on his left, And the i-1-j stand on his right. The number of schemes for choosing J from an individual in a I-1 is expressed as C (I-1, J) because j individual's high and low staggered arrangement number and the number of i-1-j arrangement is known or has been launched in the front, so I individual arrangement number is:DP [I] = (DP [J]> 1) * (DP [i-1-j]> 1) * C (I-1, J ).When the number of people is small, we can directly see the number of orders:DP [1] = 1, DP [2] = 2, DP [3] = 4;Therefore, the recurrence can start from n = 4, which is the initial condition of recurrence. Note: (1) when j = 1 or i-1-j = 1, Special Judgment is required. Because DP [1] = 1. (2) use the _ int64ac code:

# Include <cstdio> # include <iostream> # include <cstring> using namespace STD ;__ int64 DP [21] ;__ int64 POW (int n) // calculate the factorial {_ int64 sum = 1; for (INT I = 2; I <= N; I ++) sum * = I; return sum ;} __int64 C (int n, int m) // calculates the number of combinations {return POW (N)/POW (m)/POW (n-m );} // formula C (m, n) = n! /(M! * (N-m )!) Int main () {int T, Cas, T, I, J; memset (DP, 0, sizeof (DP); DP [1] = 1, DP [2] = 2, DP [3] = 4; for (I = 4; I <= 20; I ++) {DP [I] + = DP [I-1]; // The highest on both sides * C (I, 0) for (j = 1; j <I-1; j ++) {If (j = 1 | i-j-1 = 1) // if only one person on the left or only one person on the right DP [I] + = (DP [J] * DP [i-j-1]> 1) * C (I-1, J ); else DP [I] + = (DP [J]> 1) * (DP [i-j-1]> 1) * C (I-1, J );} // high-low and low-low sorting methods each account for 1/2 of the total sorting method} // both sides can only be high or low to ensure that a person can be inserted into the scanf ("% d ", & T); While (t --) {scanf ("% d", & CAS, & T); printf ("% d % i64d \ n", Cas, DP [T]);} return 0 ;}

9008101

2013-08-19 20:03:12

Accepted

4489

0 ms

228 K

791 B

C ++

Another way is to separate the left and right, and open a two-dimensional array DP [21] [2], DP [] [0] to indicate the number of rows on the left, DP [] [1] indicates the number of arrangement schemes on the right.
The number of combinations is also directly used in the two-dimensional array C [I] [J] To represent J of I.
AC code:

# Include <cstdio> # include <iostream> # include <cstring> using namespace STD ;__ int64 DP [21] [2], sum; __int64 C [21] [21]; int main () {int T, Cas, X, I, j; for (I = 1; I <21; I ++) {c [I] [0] = 1; // C [I] [J] indicates the number of solutions selected by J in I. C [I] [I] = 1; for (j = 1; j <I; j ++) C [I] [J] = C [I-1] [J-1] + C [I-1] [J]; // formula, combined with Yang Hui's triangle understanding} DP [1] [1] = DP [1] [0] = DP [0] [0] = DP [0] [1] = 1; for (I = 2; I <21; I ++) {sum = 0; For (j = 0; j <I; j ++) sum + = DP [J] [0] * DP [i-1-j] [1] * C [I-1] [J]; DP [I] [0] = sum /2; // The following recursion uses DP [I] [1] = sum/2;} scanf ("% d", & T); While (t --) {scanf ("% d", & CAS, & X); printf ("% d % i64d \ n", Cas, x> 1? DP [x] [1] * 2:1);} return 0 ;}

9008284

2013-08-19 20:19:06

Accepted

4489

0 ms

232 K

688 B

C ++



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.