当前位置:网站首页>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]]);
}
}
边栏推荐
- FreeRTOS(八)——时间管理
- Cloud management platform: openstack architecture design and detailed interpretation
- 367. effective complete square dichotomy
- Segmentation of Head and Neck Tumours Using Modified U-net
- leetcode MYSQL数据库题目180
- Causes and solutions of error reporting by using startactivity() method outside the activity
- Invalidconnectionattributeexception exception exception handling
- Please use the learned knowledge to write a program to find out the password hidden in the long string below. The burial point of the password conforms to the following rules:
- 監控數據源連接池使用情况
- Gmail: how to quickly read all messages
猜你喜欢

linux环境下安装配置redis,并设置开机自启动

Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear

C语言实现一种创建易管理易维护线程的方法

遍历vector容器中的对象的方式

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

JVM之TLAB

自定义mvc框架实现

IPC(进程间通信)之管道详解

JVM之对象的内存布局

2020-09-29 非商品模板化代码层次 rapidjson库
随机推荐
LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
Lc236. nearest common ancestor of binary tree
ImageView picture fill problem
Pointer functions and function pointers
KDevelop new project
[technology development] development and design of alcohol tester solution
zabbix4.4配置监控服务器指标,以及图形页乱码解决
Mysql5.7 installation tutorial in centos7 under Linux
MySQL configuring master-slave databases
JVM之虚拟机栈之动态链接
2020-09-25 boost库的noncopyable,用于单例模式
证券账号开户安全吗?是靠谱的吗?
2020-09-17 gateway业务流程 两个任务:referer认证和非商品模板化
数据源连接池未关闭的问题 Could not open JDBC Connection for transaction
In XML layout, the button is always displayed on the top layer
聊聊你理解的线程与并发
General part: cognition, design and best practice of prototype design
FreeRTOS (IX) - queue
RecyclerView 通用适配器封装
Set up lamp environment under cenos7