Mr. Young's Picture Permutations

给出一个有k列的网格图,以及每列图形的高度\(n_i\),下端对齐,保证高度递减,设有n个网格,询问向其中填1~n保证每行每列单调递增的方案数,\(n\leq 30,k\leq 5\)。

事实上,注意到k很小,我们就可以暴力状态了,而要表现单调递增,故维护一个从左至右边矮的阶梯。

以有5列为例,设\(f[a][b][c][d][e]\)表示第1列已经填的数字高度为a,第二列高度为b,...,第5列的高度为e的,现在填到数字\(a+b+c+d+e\)方案数,并保证转移时\(a\geq b\geq c\geq d\geq e\),于是当一个新的数字填的时候一定比所有的数字都要大,也不会有比它小的数字使其非法,故满足了题意。

有因为逆转移无用状态枚举过多,考虑顺转移,因此有

\[f[a+1][b][c][d][e]+=f[a][b][c][d][e]
\]

\[f[a][b+1][c][d][e]+=f[a][b][c][d][e]
\]

\[f[a][b][c+1][d][e]+=f[a][b][c][d][e]
\]

\[f[a][b][c][d+1][e]+=f[a][b][c][d][e]
\]

\[f[a][b][c][d][e+1]+=f[a][b][c][d][e]
\]

边界:\(f[0][0][0][0][0]=1\),其余为0

答案:\(f[n_1][n_2][n_3][n_4][n_5]\)

参考代码:

阶段转移

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
#define ll long long
using namespace std;
ll len[31],dp[31][16][11][8][7];
il void read(ll&);
int main(){
ll k,n;
while(read(k),k){
memset(len,0,sizeof(len));
memset(dp,0,sizeof(dp)),dp[0][0][0][0][0]=1;
for(ri ll i(1);i<=k;++i)read(len[i]);
for(ri ll a,b,c,d,e(0);e<=len[5];++e)
for(d=e;d<=len[4];++d)
for(c=d;c<=len[3];++c)
for(b=c;b<=len[2];++b)
for(a=b;a<=len[1];++a){
if(a<len[1])dp[a+1][b][c][d][e]+=dp[a][b][c][d][e];
if(b<len[2])dp[a][b+1][c][d][e]+=dp[a][b][c][d][e];
if(c<len[3])dp[a][b][c+1][d][e]+=dp[a][b][c][d][e];
if(d<len[4])dp[a][b][c][d+1][e]+=dp[a][b][c][d][e];
if(e<len[5])dp[a][b][c][d][e+1]+=dp[a][b][c][d][e];
}
printf("%lld\n",dp[len[1]][len[2]][len[3]][len[4]][len[5]]);
}
return 0;
}
il void read(ll &x){
x&=0;ri char c;while(c=getchar(),c<'0'||c>'9');
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

dfs

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
#define ll long long
using namespace std;
int len[6];
ll dp[31][16][11][8][7];
il void read(int&);
ll dfs(int,int,int,int,int);
int main(){
int k;while(read(k),k){
memset(len,0,sizeof(len));
memset(dp,0,sizeof(dp)),dp[0][0][0][0][0]=1;
for(ri int i(1);i<=k;++i)read(len[i]);
printf("%lld\n",dfs(len[1],len[2],len[3],len[4],len[5]));
}
return 0;
}
ll dfs(int a,int b,int c,int d,int e){
if(a<0||b<0||c<0||d<0||e<0)return 0;
ll &opt=dp[a][b][c][d][e];if(opt)return opt;
opt=dfs(a,b,c,d,e-1);
if(d>e)opt+=dfs(a,b,c,d-1,e);
if(c>d)opt+=dfs(a,b,c-1,d,e);
if(b>c)opt+=dfs(a,b-1,c,d,e);
if(a>b)opt+=dfs(a-1,b,c,d,e);
return opt;
}
il void read(int &x){
x&=0;ri char c;while(c=getchar(),c<'0'||c>'9');
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Mr. Young's Picture Permutations的更多相关文章

  1. bzoj 2483: Pku2279 Mr. Young's Picture Permutations -- 钩子公式

    2483: Pku2279 Mr. Young's Picture Permutations Time Limit: 1 Sec  Memory Limit: 128 MB Description   ...

  2. POJ2279 Mr Young's Picture Permutations

    POJ2279 Mr Young's Picture Permutations 描述: 有N个学生合影,站成左对齐的k排,每行分别有N1,N2…NK个人,第一排站最后,第k排站之前.学生身高依次是1… ...

  3. 【题解】POJ2279 Mr.Young′s Picture Permutations dp

    [题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...

  4. 轮廓线DP:poj 2279 Mr. Young's Picture Permutations

    poj 2279 Mr. Young's Picture Permutations \(solution:\) 首先摘取一些关键词:(每行不超过它后面的行)(每排学生安排高度从左到右减少)(学生的高度 ...

  5. 【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations

    Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...

  6. poj2279——Mr. Young's Picture Permutations

    Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...

  7. [POJ 2279] Mr. Young's Picture Permutations

    [题目链接] http://poj.org/problem?id=2279 [算法] 杨氏矩阵与勾长公式 [代码] #include <algorithm> #include <bi ...

  8. POJ P2279 Mr. Young's Picture Permutations 题解

    每日一题 day14 打卡 Analysis 五维dpf[a1,a2,a3,a4,a5]表示各排从左端起分别占了a1,a2,a3,a4,a5个人时合影方案数量然后我们枚举a1,a2,a3,a4,a5从 ...

  9. poj2279 Mr. Young's Picture Permutations[勾长公式 or 线性DP]

    若干人左对齐站成最多5行,给定每行站多少个,列数从第一排开始往后递减.要求身高从每排从左到右递增(我将题意篡改了便于理解233),每列从前向后递增.每个人身高为1...n(n<=30)中的一个数 ...

随机推荐

  1. 原生js - 两种图片懒加载实现原理

    目前图片懒加载的方式主要有两种: 1.利用getBoundingClientRectAPI得到当前元素与视窗的距离来判断 2.利用h5的新API IntersectionObserver 来实现 ge ...

  2. 堡垒机介绍及实现 (使用python django实现)(一)

    堡垒机介绍及实现 (使用python django实现)(一) 堡垒机的功能 我们在使用服务器的时候,通常的方式是 ssh user@ip 然后输入password 多人同时使用,就需要多个账号.这时 ...

  3. [转]C++的Json解析库:jsoncpp和boost

    JSON(JavaScript Object Notation)跟xml一样也是一种数据交换格式,了解json请参考其官网http://json.org,本文不再对json做介绍,将重点介绍c++的j ...

  4. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers

    how do I install all developer tools such as GNU GCC C/C++ compilers, make and others, after install ...

  5. win7 设置双屏壁纸

    http://dualmonitortool.sourceforge.net/ http://www.displayfusion.com/Features/Wallpaper/ 以第一个软件为例: 1 ...

  6. PAT_A1043#Is It a Binary Search Tree

    Source: PAT A1043 Is It a Binary Search Tree (25 分) Description: A Binary Search Tree (BST) is recur ...

  7. CentOS7 相关配置

    nginx 1.在线安装nginx yum install nginx 2.启动nginx服务 systemctl start nginx 3.防火墙设置 打开http防火墙:firewall-cmd ...

  8. python 19 lambda函数

    转自http://www.cnblogs.com/BeginMan/p/3178103.html 一.lambda函数 1.lambda函数基础: lambda函数也叫匿名函数,即,函数没有具体的名称 ...

  9. Qt5 linux下的配置

    对于用Qt开发图形界面,Qt会用到openGL的相关库文件和头文件.虽然绝大多数的linux发行版中都没有预置安装这些开发工具,但是要安装它们,也是非常简单的.用一行安装命令即可安装完毕. Debia ...

  10. 科普帖:深度学习中GPU和显存分析

    知乎的一篇文章: https://zhuanlan.zhihu.com/p/31558973 关于如何使用nvidia-smi查看显存与GPU使用情况,参考如下链接: https://blog.csd ...