HDU 4689 Derangement【DP递推】【好题】【思维题】
来源:互联网 发布:百度php面试题 编辑:程序博客网 时间:2024/05/20 03:43
Derangement
Time Limit: 7000/7000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 1170 Accepted Submission(s): 396
Problem Description
A derangement is a permutation such that none of the elements appear in their original position. For example, [5, 4, 1, 2, 3] is a derangement of [1, 2, 3, 4, 5]. Subtracting the original permutation from the derangement, we get the derangement difference [4, 2, -2, -2, -2], where none of its elements is zero. Taking the signs of these differences, we get the derangement sign [+, +, -, -, -]. Now given a derangement sign, how many derangements are there satisfying the given derangement sign?
Input
There are multiple test cases. Process to the End of File.
Each test case is a line of derangements sign whose length is between 1 and 20, inclusively.
Each test case is a line of derangements sign whose length is between 1 and 20, inclusively.
Output
For each test case, output the number of derangements.
Sample Input
+-++---
Sample Output
113
Author
Zejun Wu (watashi)
Source
2013 Multi-University Training Contest 9
题意:1到n的n个数,打乱后减去原来的数的正负号,形成一串正负号的字符串,如[1,2,3,4,5]打乱后变成[5,4,1,2,3]就是[+,+,-,-,-]。给定这样一串正负号形成的字符串,有几种排列方式。
dp[i][j]表示前i个位置中有j个空位(+号)没有确定,负号全部确定。
“+'时
要么使空位增加,要么不变
增加时,直接空位增加:dp[i][i]+=dp[i-1][j-1]
不变时,表示把当前的数填到前面未确定的j个空位中去:dp[i][j]+=dp[i-1][j]*j
‘-'时,因为必须确定
要么减少,要么不变
不变时,表示把当前数填到前面的j个空位中:dp[i][j]+=dp[i-1][j]*j
减少时,表示把当前数填到前j-1位中去,再从前j-1位中拿一个数填到当前位:dp[i][j]+=dp[i-1][j-1]*(j-1)*(j-1)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define ll long longll dp[30][30];char num[100];int main(){while (~scanf("%s", num)){int len = strlen(num);memset(dp, 0, sizeof(dp));dp[0][0] = 1;for (int i = 1; i <= len; i++){if (num[i - 1] == '+'){for (int j = 0; j <= i; j++){dp[i][j] += dp[i - 1][j - 1];dp[i][j] += dp[i - 1][j] * (ll)j;}}else{for (int j = 0; j <= i; j++){// dp[i][j - 1] += dp[i - 1][j] * j * j;dp[i][j] += dp[i - 1][j] * (ll)j;dp[i][j] += dp[i - 1][j + 1] * (ll)(j + 1) * (ll)(j + 1);}}}// printf("%lld\n", dp[len][0]);cout << dp[len][0] << endl;}return 0;}
阅读全文
0 0
- HDU 4689 Derangement【DP递推】【好题】【思维题】
- HDU 4689 Derangement【DP】
- HDU 6143 Killer Names【dp递推】【好题】【思维题】【阅读题】
- HDU 6078 Wavel Sequence【dp递推】【好题】【思维题】
- hdu 4689 Derangement(dp)
- 51nod 1020 逆序排列【Dp+思维递推优化】好题!好题!好题!
- Hrbust 1846 方格涂色【dp+递推思维】好题!好题!好题!
- ZZULIOJ 2134 维克兹的进制转换【思维+递推/Dp】好题!好题!
- hdu 4472 Count【思维+dp】好题
- 【队内练习赛】OMG爱吃鸡 【dp递推】【思维题】【好题】
- HDU 4055 Number String【DP递推+递推优化】【好题】
- HDOJ 4689 Derangement DP
- HDU 4689 Derangement
- hdu 4689 Derangement
- Hdu 6017 Girls Love 233【Dp+思维】好题~
- Hdu 2476 String painter【思维+区间Dp】好题~
- hdu 2583 permutation 好题 递推
- hdu 5965(dp递推,简单题)
- 数组中重复的数字
- 【Cocos2d-x】开发基础-第一个Cocos2d-x游戏
- echarts动态报表
- 插入排序链表
- ActiveMQ集群:网络连接模式(network connector)详解 .
- HDU 4689 Derangement【DP递推】【好题】【思维题】
- 2. 线性结构--栈
- tensorflow1.1/线性回归
- 建立自己的Github Repository
- 设计模式--CGLib动态代理的实现与解析
- Android MPChart 可跨屏幕可滑动 扩展项目 OpenCodeMpSiChart
- 浅谈JavaScript之DOM
- 逻辑设计中多时钟设计【1】
- 利用jsoup的选择器语法来查找元素