uva1638(递推关系,dp)
2016-10-06 17:27
337 查看
/* translation: 高为1...n的杆子排成一列,从左能看到l根,从右能够看到r根。求有多少排列的可能? solution: dp,递推 为了状态的转移无后效性,按照杆子的长短从小到大来排列,先排列最短的杆子。因为这样无论这个杆子放在哪里 都对后面的转态是如何转移的无任何影响。若是最短的杆子放在最右边,则接下来的状态成了dp[n-1][l][r-1] 同理,放在最左边则是dp[n-1][l-1][r],若是放在中间的话,有n-2个位置可供选择,则接下来的状态为 dp[n-1][l][r]*(n-2).从而有递推方程:dp [l][r] = dp[n-1][l-1][r] + dp[n-1][l][r-1] + dp[n-1][l][r]*(n-2); note: 1:当发现一个状态很难转移到另外一个状态时,就要考虑从两方面来入手,一是改变当前的状态表示方法,二是改变递推 方式,分析题中状态怎样转移才能不对后面的状态形成干扰。无论怎样都是为了一个目的:无后效性!! date: 2016.10.6 */ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 25; typedef long long ll; ll dp[maxn][maxn][maxn]; void printTable() { memset(dp, 0, sizeof(dp)); dp[1][1][1] = 1; for(int n = 2; n <= 20; n++) { for(int l = 1; l <= n; l++) { for(int r = 1; r+l-1 <= n; r++) { dp [l][r] = dp[n-1][l-1][r] + dp[n-1][l][r-1] + dp[n-1][l][r]*(n-2); } } } } int main() { printTable(); int T; cin >> T; int n, l, r; while(T--) { cin >> n >> l >> r; cout << dp [l][r] << endl; } return 0; }
相关文章推荐
- Uva 1638 Pole Arrangement 数学,递推,dp
- uva 11375 Matches 递推关系||DP||高精度
- 递推关系的运用加简单DP【UVA11137Ingenuous Cubrency】-------2015年1月27日
- UVa 1638 Pole Arrangement (递推或DP)
- 递推关系( Recurrences, UVa 10870)(矩阵快速幂)
- 递推DP UVA 1424 Salesmen
- 例题9-10 UVA 1626 Brackets sequence (dp递推 || 记忆化搜索)
- 递推DP UVA 607 Scheduling Lectures
- uva1638(递推)
- UVALive - 3882 And Then There Was One (递推[dp])
- UVa 11137 - Ingenuous Cubrency (递推 DP 多重背包 )
- POJ3181 dp递推关系小优化 高精度数模板 Java练习
- Uva 1638 Pole Arrangement DP
- 概率DP,递推(麻球繁殖,UVA 11021)
- UVA 10870 递推关系 矩阵快速幂
- UVA11401 Triangle Counting(递推,dp)
- poj3181 dp 优化递推关系+高精度
- 递推DP UVA 1291 Dance Dance Revolution
- uva11464(递推关系)
- 动态规划,递推(杆子的排列,uva 1638)