Mr. Young's Picture Permutations
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[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的更多相关文章
- bzoj 2483: Pku2279 Mr. Young's Picture Permutations -- 钩子公式
2483: Pku2279 Mr. Young's Picture Permutations Time Limit: 1 Sec Memory Limit: 128 MB Description ...
- POJ2279 Mr Young's Picture Permutations
POJ2279 Mr Young's Picture Permutations 描述: 有N个学生合影,站成左对齐的k排,每行分别有N1,N2…NK个人,第一排站最后,第k排站之前.学生身高依次是1… ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
- 轮廓线DP:poj 2279 Mr. Young's Picture Permutations
poj 2279 Mr. Young's Picture Permutations \(solution:\) 首先摘取一些关键词:(每行不超过它后面的行)(每排学生安排高度从左到右减少)(学生的高度 ...
- 【杨氏矩阵+勾长公式】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 ...
- 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 ...
- [POJ 2279] Mr. Young's Picture Permutations
[题目链接] http://poj.org/problem?id=2279 [算法] 杨氏矩阵与勾长公式 [代码] #include <algorithm> #include <bi ...
- 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从 ...
- poj2279 Mr. Young's Picture Permutations[勾长公式 or 线性DP]
若干人左对齐站成最多5行,给定每行站多少个,列数从第一排开始往后递减.要求身高从每排从左到右递增(我将题意篡改了便于理解233),每列从前向后递增.每个人身高为1...n(n<=30)中的一个数 ...
随机推荐
- 源码安装zabbix-oracle
源码安装zabbix_agent4.0.3 1.源码包下载地址:https://www.zabbix.com/download_sources 2.下载完后上传在任意目录用root用户创建以下脚本 ...
- Centos6.5安装ruby2.2.3
一.安装库 Yum install –y gcc* openssl* wget 二.安装ruby wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby- ...
- Delphi 2010 中的泛型
Delphi 2010 中的泛型 2010已发布很长时间了,口碑还不错,准备用它开发下一项目,但对泛型等新东西的认识还不够,就搜了一下,发现下面这篇文章,还不错,大家一起补补课吧! C++中的模板.C ...
- (c# )操作Excel的时候出现:不能使用对象或链接
可能就是你打开了多个Excel进程没有关掉出现的问题. 编程:对文件操作的时候要养成关掉进程的习惯 在c#中操作完后关闭资源的代入如下: System.Runtime.InteropServices. ...
- ECMAScript1.3 数组 | 函数 | 作用域 | 预解析
数组array 数组可以存储很多项,有顺序,很多项形成一个集合,就是数组. 数组字面量是:[] 如何获取数组中的数据:索引/下标,数组中的第一项的索引是从0开始的. ['kay', 'andy', 1 ...
- DRF的JWT用户认证
目录 DRF的JWT用户认证 JWT的认证规则 JWT的格式 JWT认证的流程 JWT模块的导入为 JWT的使用 DRF的JWT用户认证 从根本上来说,JWT是一种开放的标准(RFC 7519), 全 ...
- pip修改官方源为豆瓣源
参考地址: https://www.jianshu.com/p/10a23d6a93c6 1.临时修改 pip install pythonModuleName -i https://pypi.dou ...
- scrapy不抓取重复的网页解决办法
1.scrapy爬虫遇到一个问题,有时候会对一个网页重复爬取提取不同的数据,这时候会发现,后面的那个重复爬取scrapy直接终止了. 原因: scrapy 的request逻辑里面 dont_fil ...
- MySQL - 锁等待超时与information_schema的三个表
引用地址:https://blog.csdn.net/J080624/article/details/80596958 回顾一下生产中的一次MySQL异常,Cause: java.sql.SQLExc ...
- PHP算法之分割平衡字符串
在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...