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. 4.RabbitMQ Linux安装

    这里使用的Linux是CentOS6.2 将/etc/yum.repo.d/目录下的所有repo文件删除 先下载epel源 # wget -O  /etc/yum.repos.d/epel-erlan ...

  2. C不同变量类型存储大小引发的BUG

    #include"stdio.h" typedef signed char int8; typedef unsigned char uint8; typedef signed sh ...

  3. sql准确判断某个ip

    问题:如图 当我执行sql要准确查找某个IP是属于哪个库室时候,我刚开始是这样写的 select * from Definition_Read_Room where HFIP like '%172.2 ...

  4. Selenium(一)---Selenium的安装和使用

    一.前言 最近在帮一个老师爬取网页内容,发现网页是动态加载的,为了拿到全部的网页数据,这里使用到了Selenium.Selenium 是一个用于Web应用程序测试的工具,它可以模拟真实浏览器,支持多种 ...

  5. Thread-per-Message 这个工作交给你了

    Per是“每一”的意思,所以thread per message解释过来就是“每个消息一个线程”,message在这里可以看做是“命令”或“请求”的意思,对每隔命令或请求,分配一个线程,有这个线程执行 ...

  6. redis集群的学习(一)

    redis配置文件详解 redis默认是不作为守护进程来运行的,你可以把这个设置为yes,让它作为守护进程来运行 注意,当作为守护进程的时候,redis 会把进程ID 写到/var/run/redis ...

  7. SQL一些记录

    1,2字段约束create unique index [索引名] on 软件信息表(S_SName,S_Edition)

  8. Python短信电话报警

    sid 和token 需要自己去https://www.twilio.com/try-twilio注册twilio 账号申请是免费的  from后面的电话也是官方提供的 直接看脚本 # -*-cond ...

  9. django笔记(python web框架)

    1.Python 下载地址:https://www.python.org/downloads/ 2.Django 下载地址:https://www.djangoproject.com/download ...

  10. python 毫秒时间戳转日期

    import time import datetime timestamp = 1570774556514 # 转换成localtime time_local = time.localtime(tim ...