当前位置:网站首页>acwing271【杨老师的照相排列】【线性DP】
acwing271【杨老师的照相排列】【线性DP】
2022-06-29 09:17:00 【叶卡捷琳娜2号】
有 N 个学生合影,站成左端对齐的 k 排,每排分别有 N1,N2,…,Nk 个人。 (N1≥N2≥…≥Nk)
第1排站在最后边,第 k 排站在最前边。
学生的身高互不相同,把他们从高到底依次标记为 1,2,…,N。
在合影时要求每一排从左到右身高递减,每一列从后到前身高也递减。
问一共有多少种安排合影位置的方案?
下面的一排三角矩阵给出了当 N=6,k=3,N1=3,N2=2,N3=1 时的全部16种合影方案。注意身高最高的是1,最低的是6。
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
输入格式
输入包含多组测试数据。
每组数据两行,第一行包含一个整数k表示总排数。
第二行包含k个整数,表示从后向前每排的具体人数。
当输入k=0的数据时,表示输入终止,且该数据无需处理。
输出格式
每组测试数据输出一个答案,表示不同安排的数量。
每个答案占一行。
数据范围
1≤k≤5,学生总人数不超过30人。
输入样例:
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
输出样例:
1
1
16
4158
141892608
9694845
思路:我们从高到低依次考虑每位同学所站位置,按照某种原则进行站队,在这种顺序下就能保证学生在每一行每一列中身高都是单调递减的
这种原则满足两个条件:
1.所站行还没有占满
2.所站行为最后一排或者所站行的人数比其后排人数少。
假设现在轮到某个(第a+b+c+d+e个)同学进入队伍,他选择了第一排,5排人数原来有f[a-1,b,c,d,e]人,a-1,b,c,d,e均大于等于零 则有f[a,b,c,d,e] += f[a-1,b,c,d,e],如果选择第二排且满足以上两个条件,就有f[a,b,c,d,e]+=f[a,b-1,c,d,e];
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 31;
typedef long long ll;
ll f[N][N][N][N][N];
int main(){
int n;
while(~scanf("%d",&n),n){
memset(f,0,sizeof(f));
int s[5]={
0};
for(int i=0;i<n;i++) scanf("%d",&s[i]);
f[0][0][0][0][0]=1;
for(int a=0;a<=s[0];a++)
for(int b=0;b<=min(a,s[1]);b++)
for(int c=0;c<=min(b,s[2]);c++)
for(int d=0;d<=min(c,s[3]);d++)
for(int e=0;e<=min(e,s[4]);e++){
ll &temp=f[a][b][c][d][e];
if(a&&a-1>=b) temp+=f[a-1][b][c][d][e];
if(b&&b-1>=c) temp+=f[a][b-1][c][d][e];
if(c&&c-1>=d) temp+=f[a][b][c-1][d][e];
if(d&&d-1>=e) temp+=f[a][b][c][d-1][e];
if(e) temp+=f[a][b][c][d][e-1];
}
printf("%lld\n",f[s[0]][s[1]][s[2]][s[3]][s[4]]);
}
}
边栏推荐
- Construction and use of Changan chain go language smart contract environment
- Recyclerview refreshes blinks and crashes when deleting items
- 监控数据源连接池使用情况
- Gmail:如何快速将邮件全部已读
- 请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
- 2020-09-18 referer认证 url转义
- Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
- Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
- Data governance: data standard management (Part III)
- Custom MVC framework implementation
猜你喜欢

Custom MVC framework implementation

Segmentation of Head and Neck Tumours Using Modified U-net

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

Middle order traversal of Li Kou 94 binary tree

力扣94二叉树的中序遍历

Force deduction 85 question maximum rectangle

Constructing SQL statements by sprintf() function in C language

Pipeline details of IPC (interprocess communication)

Flutter 基础组件之 ListView

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
随机推荐
Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail
在Activity外使用startActivity()方法报错原因与解决办法
C语言实现一种创建易管理易维护线程的方法
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
Perfect binary tree, complete binary tree, perfect binary tree
JVM之 MinorGC、 MajorGC、 FullGC、
Data governance: the solution of data governance in the data Arena
Application of decorator mode, packaging ServletRequest and adding addparameter method
请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
Construction and use of Changan chain go language smart contract environment
Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
FreeRTOS (IX) - queue
Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
linux下centos7中mysql5.7安装教程
Cisco ASA、FTD和HyperFlex HX的漏洞分析复现
2020-09-21 referer字符串切分 boost gateway代码组织层次
指针函数和函数指针
数据源连接池未关闭的问题 Could not open JDBC Connection for transaction
Could not open JDBC connection for transaction
linux环境下安装配置redis,并设置开机自启动