UVa 1638 Pole Arrangement (recursive or DP)

Source: Internet
Author: User

Test instructions: There is a high for the 1,2,3...N pole each row, from the left can see L root, from the right can see R root, to find out how many kinds of pole arrangement is possible.

Analysis: Set D (i, J, K) to indicate that a pole with a height of 1-i is lined up, see J Root from the left, and see the number of K roots from the right. When I>1, we press the pole in order from big to small,

Assuming that the i-1 root has been arranged, then there is a height of 1, then it will not block any pole. There are 3 types of cases:

1. On the left, you will be able to see it on the left and not see it on the right (because i>1);

2. On the right side, you will be able to see it on the right, not on the left (because I>1);

3. The rest is put in the middle, then we have how many places can be put, the answer is obvious, I-2, can not see it on either side.

Situation 1 o'clock, in the height in the 2-i pole, on the left can see J-1 root, from the right can see K root, because on the left with the first root is just J root,

Similarly, the case 2, in the height of the 2-i pole, on the left can see J Root, from the right can see k-1 root, because on the right with the first root is just k root,

What about Scenario 3? Better said, in the height of the pole in the 2-i, on the left can see J Root, from the right can see K root, because on the left and right are not see the first root.

Comprehensive analysis: D (i, j, k) = d (I-1, j-1, K) + D (I-1, J, K-1) + D (I-1, J, K) * (i-2), boundary is D (1, 1, 1) = 1;

You can first sweep out the situation first, when the direct query on the line, fast and convenient. Note that more than int is used to store a long long.

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #define MoD%10056using namespace Std;typedef Long Long LL; LL d[25][25][25];void init () {    memset (d, 0, sizeof (d));    D[1][1][1] = 1;    for (int i = 2; I <=, ++i) for        (int j = 1; J <=, ++j) for            (int k = 1; k <=; ++k)                d[i][j][k] = d [I-1] [J-1] [K] + d[i-1][j][k-1] + d[i-1][j][k] * (i-2);} int main () {    init ();    int T, N, L, R;  Cin >> T;    while (t--) {        scanf ("%d%d%d", &n, &l, &r);        printf ("%lld\n", D[n][l][r]);    }    return 0;}

UVa 1638 Pole Arrangement (recursive or DP)

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.