首页 > 代码库 > 线性DP POJ2279 Mr.Young's Picture Permutations
线性DP POJ2279 Mr.Young's Picture Permutations
Mr. Young‘s Picture Permutations
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1128 | Accepted: 562 |
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.
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.):
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:
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.
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
这个题嘛,标准做法是线性DP。
f[a1,a2,a3,a4,a5]表示每排从左起占了a1,a2,a3,a4,a5个人的方案数,f[0,0,0,0,0]=1。
转移方程为:当a1<N1,f[a1+1,a2,a3,a4,a5]+=f[a1,a2,a3,a4,a5],其余同理。
那么简单做法就是:先去了解一下杨氏矩形和勾长公式,然后直接用公式做。我这种蒟蒻就选了这种方法……
杨氏矩阵定义(需满足的条件/特征):(1)若格子(i,j),则该格子的右边和上边一定没有元素;
(2)若格子(i,j)有元素data[i][j],则该格子右边和上边相邻的格子要么没有元素,要么有比data[i][j]大的元素。
显然有同已写元素组成的杨氏矩阵不唯一,1~n组成杨氏矩阵的个数可以写出:F[1]=1,F[2]=2,F[n]=F[n-1]+(n-1)*F[n-2] (n>2)。
钩子长度的定义:该格子右边的格子数和它上边的格子数之和;
钩子公式:对于给定形状,不同的杨氏矩阵的个数为(n!/(每个格子的钩子长度加1的积))。
知道了这些再做这个题就很方便了……
代码
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define int __int64 6 using namespace std; 7 int n,cnt,x,y,tmp; 8 int num[40],sum[1600]; 9 int gcd(int a,int b){ 10 if(a%b==0) return b; 11 return gcd(b,a%b); 12 } 13 signed main(void){ 14 while(scanf("%d",&n)&&n){ 15 memset(sum,0,sizeof(sum)); 16 cnt=0; 17 x=1; 18 y=1; 19 for(int i=1;i<=n;i++) scanf("%d",&num[i]); 20 for(int i=1;i<=n;i++) 21 for(int j=1;j<=num[i];j++){ 22 cnt++; 23 for(int k=i+1;k<=n;k++){ 24 if(num[k]>=j) sum[cnt]++; 25 else break; 26 } 27 sum[cnt]+=num[i]-j+1; 28 } 29 for(int i=1;i<=cnt;i++){ 30 x*=i; 31 y*=sum[i]; 32 tmp=gcd(x,y); 33 x/=tmp; 34 y/=tmp; 35 } 36 printf("%I64d\n",x/y); 37 } 38 return 0; 39 }
线性DP POJ2279 Mr.Young's Picture Permutations
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。