UVa11077-Find the Permutations(dp+置换)
来源:互联网 发布:行程编码压缩算法 编辑:程序博客网 时间:2024/05/18 20:04
题目链接
分析:
先考虑一个简单一点的问题:
给出一个序列,至少需要交换多少次才能变成{1,2,3,…,n}
我们可以直接把这个序列理解成一个置换
把ta分解成若干轮换的乘积
对于一个拥有x个元素的轮换,我们需要交换x-1次才能达到题目的要求
所以说,如果这个置换有sum个轮换
那么需要的总交换次数是n-sum
那么现在的问题就是求出全排列对应的置换的轮换数
这个问题可以用dp解决:
设计状态 f[i][j]
表示长度为i的序列,需要j次交换才能有序
转移:
f[i][j]=f[i-1][j]+f[i-1][j-1]*(i-1)
元素i要么自己是一个轮换,不需要交换
要么加入之前元素中的任一个轮换,并贡献一次交换
tip
为什么没有1A呢?
因为我的输入终止判断应该是
while (n)
k可以等于0
//这里写代码片#include<cstdio>#include<cstring>#include<iostream>#define ll unsigned long long using namespace std;ll f[30][30];int n,k;int main(){ //f[i][j] 需要至少j步才能到达{1,2,3,...,i} f[1][0]=1; for (int i=2;i<=21;i++) for (int j=0;j<i;j++) { f[i][j]+=f[i-1][j]; if (j) f[i][j]+=f[i-1][j-1]*(i-1); } scanf("%d%d",&n,&k); while (n) { printf("%llu\n",f[n][k]); scanf("%d%d",&n,&k); } return 0;}
阅读全文
0 0
- UVa11077-Find the Permutations(dp+置换)
- Find the Permutations UVA11077
- uva11077 Find the Permutations
- uva11077 Find the Permutations
- UVA 11077 Find the Permutations(置换+dp)
- uva 11077 - Find the Permutations(置换+dp)
- UVA 11077 Find the Permutations(置换+dp)
- UVA11077 Find the Permutations(排列统计)
- UVA Find the Permutations 11077 (DP&置换群)
- UVa 11077 Find the Permutations / 置换
- uva 11077 - Find the Permutations(置换)
- UVA - 11077 Find the Permutations (置换)
- UVA 11077 Find the Permutations(置换)
- UVA 11077 Find the Permutations 置换+递推
- uva 11077 Find the Permutations 置换+递推
- UVA 11077 Find the Permutations DP
- Find the Permutations [Uva 11077]
- uva11077
- 天天学Linux命令32--/etc/group文件详解
- Unity动画系统 动态添加动画事件
- 七件你不得不知的Linux小技巧
- Mac下改动Android Studio 所用的JDK版本号
- jq 常用正则表达式
- UVa11077-Find the Permutations(dp+置换)
- android 持久存储
- JavaScript验证页面上动态生成的Radio
- Linux 磁盘与文件系统 —— 基础知识(一)
- 堆排序的实现
- 序列容器之 heap
- Oracle数据库cmd控制台创建用户和权限
- js 获取农历的代码
- php如何判断SQL语句的查询结果是否为空?