poj2279 Mr. Young‘s Picture Permutations

Problem Portal: http://poj.org/problem?id=2279

Teacher Yang's photo arrangement

Description

Mr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behind it and the left ends of the rows aligned. For instance, 12 students could be arranged in rows (from back to front) of 5, 3, 3 and 1 students.

X X X X X

X X X

X X X

X

In addition, Mr. Young wants the students in each row arranged so that heights decrease from left to right. Also, student heights should decrease from the back to the front. Thinking about it, Mr. Young sees that for the 12-student example, there are at least two ways to arrange the students (with 1 as the tallest etc.):

 1  2  3  4  5     1  5  8 11 12

 6  7  8           2  6  9

 9 10 11           3  7 10

12                 4

Mr. Young wonders how many different arrangements of the students there might be for a given arrangement of rows. He tries counting by hand starting with rows of 3, 2 and 1 and counts 16 arrangements:

123 123 124 124 125 125 126 126 134 134 135 135 136 136 145 146

45  46  35  36  34  36  34  35  25  26  24  26  24  25  26  25

6   5   6   5   6   4   5   4   6   5   6   4   5   4   3   3

Mr. Young sees that counting by hand is not going to be very effective for any reasonable number of students so he asks you to help out by writing a computer program to determine the number of different arrangements of students for a given set of rows.

Input

The input for each problem instance will consist of two lines. The first line gives the number of rows, k, as a decimal integer. The second line contains the lengths of the rows from back to front (n1, n2,…, nk) as decimal integers separated by a single space. The problem set ends with a line with a row count of 0. There will never be more than 5 rows and the total number of students, N, (sum of the row lengths) will be at most 30.

Output

The output for each problem instance shall be the number of arrangements of the N students into the given rows so that the heights decrease along each row from left to right and along each column from back to front as a decimal integer. (Assume all heights are distinct.) The result of each problem instance should be on a separate line. The input data will be chosen so that the result will always fit in an unsigned 32 bit integer.

Sample Input

1
30
5
1 1 1 1 1
3
3 2 1
4
5 3 3 1
5
6 5 4 3 2
2
15 15
0

Sample Output

1
1
16
4158
141892608
9694845

Source

Greater New York 2004

Five variables are used to represent the current state, and each variable represents the number of people in the current row: f[a][b][c][d][e]

Based on which line the last classmate is assigned to:

f[a][b][c][d][e] can be transferred from up to 5 states, that is, the nth person is in the first line f[a-1][b][c][d][e ], the second line f[a][b-1][c][d][e], the third line...

At the same time, the number of people who need to meet the previous line is greater than or equal to the next line

The most pitted place: open the array with the incoming data inside the main function, otherwise it will MLE

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

int num[5]; // 每一行的人数 

int main(void)
{
    
    
    int k;
    while (cin >> k && k) {
    
    
        memset(num, 0, sizeof num); // 一定要记得清空
        for (int i = 0; i < k; i++) {
    
    
            scanf("%d", &num[i]);
        }
        ll f[num[0]+1][num[1]+1][num[2]+1][num[3]+1][num[4]+1];
        memset(f, 0, sizeof f);
        f[0][0][0][0][0] = 1;
        
        for (int a = 0; a <= num[0]; a++)
            for (int b = 0; b <= min(a, num[1]); b++)
                for (int c = 0; c <= min(b, num[2]); c++)
                    for (int d = 0; d <= min(c, num[3]); d++)
                        for (int e = 0; e <= min(d, num[4]); e++) {
    
    
                        	// 引用 
                            ll &x = f[a][b][c][d][e];
                            if (a && a - 1 >= b) x += f[a - 1][b][c][d][e];
                            if (b && b - 1 >= c) x += f[a][b - 1][c][d][e];
                            if (c && c - 1 >= d) x += f[a][b][c - 1][d][e];
                            if (d && d - 1 >= e) x += f[a][b][c][d - 1][e];
                            if (e) x += f[a][b][c][d][e - 1];
                        }
        cout << f[num[0]][num[1]][num[2]][num[3]][num[4]] << endl;
    }
    
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43772166/article/details/112724640