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
很烦的是这题dp会mle,但我还是把代码放上来吧qwq
我们将每个学生按从高到低的顺序进行插入,因为存在单调性所以只需要记录人数即可,不需要详细记录我每行放了什么人。
每次插入得满足
1 此行没有超过限制的ai
2 此行人数小于上一行人数 只有这样,插入的话才能保证此学生比上一行同一个位置的学生矮
f[i][j][k][p][q]的五维数组表示一下每一行即可。
#include
#include
#include
#include
using namespace std;
int f[31][31][31][31][31],lim[6];
int n;
inline int read()
{
int ret=0;char ch=getchar();
for (;!isdigit(ch);ch=getchar());
for (;isdigit(ch);ch=getchar()) ret=ret*10+ch-'0';
return ret;
}
int main()
{
for (n=read();n;n=read())
{
memset(f,0,sizeof(f));
memset(lim,0,sizeof(lim));
for (int i=1;i<=n;i++) lim[i]=read();
f[0][0][0][0][0]=1;
for (int i=0;i<=lim[1];i++)
for (int j=0;j<=lim[2];j++)
for (int k=0;k<=lim[3];k++)
for (int p=0;p<=lim[4];p++)
for (int q=0;q<=lim[5];q++)
{
if (i+1<=lim[1]) f[i+1][j][k][p][q]+=f[i][j][k][p][q];
if (j+1<=lim[2]&&j<i) f[i][j+1][k][p][q]+=f[i][j][k][p][q];
if (k+1<=lim[3]&&k<i&&k<j) f[i][j][k+1][p][q]+=f[i][j][k][p][q];
if (p+1<=lim[4]&&p<i&&p<j&&p<k) f[i][j][k][p+1][q]+=f[i][j][k][p][q];
if (q+1<=lim[5]&&q<i&&q<j&&q<k&&q<p) f[i][j][k][p][q+1]+=f[i][j][k][p][q];
}
printf("%d\n",f[lim[1]][lim[2]][lim[3]][lim[4]][lim[5]]);
}
return 0;
}