POJ 2279 dp

76 篇文章 0 订阅

Mr. Young's Picture Permutations

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1826 Accepted: 773

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会爆空间 其实他们没算维度

五维dp 每维是队列有多少人

我们怎么放呢?只要一队一直没满 或者 一队满了 下面的队依次比上面的队少就可以塞人了

/*
POJ 2279
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
using namespace std;
int arr[6];
long long dp[31][16][11][9][6];
int main(){
   int n;
   while(scanf("%d",&n)&&n){
    memset(arr,0,sizeof(arr));
    for(int i = 1;i<=n;++i){
        scanf("%d",&arr[i]);
    }
    memset(dp,0,sizeof(dp));
    dp[0][0][0][0][0] = 1;
     for(int i = 0;i<=arr[1];++i)
        for(int j = 0;j<=arr[2];++j)
          for(int k=0;k<=arr[3];++k)
            for(int l = 0;l<=arr[4];++l)
              for(int x=0;x<=arr[5];x++){
               if(i+1<=arr[1]) dp[i+1][j][k][l][x] += dp[i][j][k][l][x];
               if(j+1<=arr[2]&&i>j) dp[i][j+1][k][l][x]+=dp[i][j][k][l][x];
               if(k+1<=arr[3]&&i>k&&j>k) dp[i][j][k+1][l][x] +=dp[i][j][k][l][x];
               if(l+1<=arr[4]&&i>l&&j>l&&k>l) dp[i][j][k][l+1][x]+=dp[i][j][k][l][x];
               if(x+1<=arr[5]&&i>x&&j>x&&k>x&&l>x) dp[i][j][k][l][x+1]+=dp[i][j][k][l][x];
     }
    printf("%lld\n",dp[arr[1]][arr[2]][arr[3]][arr[4]][arr[5]]);
   }
   return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值