HDU 4489 the king's ups and downs (DP + 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): 172 accepted submission (s): 106 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
ideas: assuming that you want to calculate ans [I] Now, the previous I-1 has been calculated, divide the number of front I-1 into two sides (J on the left, i-1-j on the right), then insert I is enough. To ensure that conditions are met after insertion, the left side must end with a valley and the right side must start with a valley. You can use DP [I] [0] and DP [I] [1] to maintain these two numbers and then push them down. The current Dp value must also be calculated during the push. the values of PS: DP [I] [0] and DP [I] [1] are equal, because the two mirrors are symmetric and correspond one to one.
feelings: DP, the most important thing is to push the equation. Think differently, dare to try.
Code :

# Include <iostream> # include <cstdio> # include <cstring> # define maxn 25 using namespace STD; int n, m; long CNT; long ans [maxn], DP [maxn] [2], C [maxn] [maxn]; void calc () // Yang Hui triangle calculates the number of combinations {int I, j; memset (C, 0, sizeof (c); for (I = 0; I <= 20; I ++) {C [I] [0] = 1 ;}for (I = 1; I <= 20; I ++) {for (j = 1; j <= 20; j ++) {c [I] [J] = C [I-1] [J] + C [I-1] [J-1] ;}} void solve () // calculates the answer {int I, j, k; ans [1] = 1; DP [0] [0] = DP [0] [1] = DP [1] [0] = DP [1] [1] = 1; for (k = 2; k <= 20; k ++) {CNT = 0; for (I = 0; I <K; I ++) {J = k-1-i; CNT + = DP [I] [0] * DP [J] [1] * C [k-1] [I];} ans [k] = CNT; // record answer DP [k] [0] = DP [k] [1] = CNT> 1; // calculate the Dp value because the following code uses} int main () {int I, j, T, test; calc (); solve (); scanf ("% d", & T); While (t --) {scanf ("% d", & test, & N ); printf ("% d % i64d \ n", test, ANS [N]);} 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.