UVALive 5790 Ball Stacking DP
2014-09-05 13:44
323 查看
DP的方向真的很重要,这题做的时候死活想不出来,看了题解以后恍然大悟原来这么简单。
题意:
有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取。求选的球最大的权值和。
题解:
把原来的金字塔变换一下形式,转换成直角三角形的样子。
假如原三角形是这样的:
View Code
题意:
有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取。求选的球最大的权值和。
题解:
把原来的金字塔变换一下形式,转换成直角三角形的样子。
假如原三角形是这样的:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <string> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <functional> #include <cctype> #include <time.h> using namespace std; const int INF = 1<<30; const int MAXN = 1111; int a[MAXN][MAXN]; int sum[MAXN][MAXN]; int dp[MAXN][MAXN]; int n; int main() { #ifdef Phantom01 freopen("LA5790.txt", "r", stdin); #endif //Phantom01 while (scanf("%d", &n)!=EOF) { if (n==0) break; memset(dp, 0, sizeof(dp)); memset(sum, 0, sizeof(sum)); for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) scanf("%d", &a[i-j+1][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n-i+1; j++) sum[i][j] = a[i][j] + sum[i][j-1] + sum[i-1][j] - sum[i-1][j-1]; int ans = 0; for (int i = 1; i <= n; i++) { int best = n-i+2; for (int j = n-i+1; j > 0; j--) { if (dp[i-1][j]>dp[i-1][best]) best = j; dp[i][j] = dp[i-1][best]+sum[i][j] - sum[i-1][j]; ans = max(ans, dp[i][j]); } } printf("%d\n", ans); } return 0; }
View Code
相关文章推荐
- UVALive 5790 Ball Stacking dp
- UVALive 5790 Ball Stacking(灵活转换)
- UVALive 5790 Ball Stacking 解题报告
- UVALive 5790 Ball Stacking 解题报告
- uvalive5790(DP)
- UVaLive/LA 6801 Sequence(DP)
- UVA Live Achrive 4327 Parade (单调队列,dp)
- UvaLive 6531 Go up the ultras DP+RMQ
- UVALive - 5088 Alice and Bob's Trip 树形DP
- UVALIVE 4857 Halloween Costumes (区间DP)
- UVALive 6042 Bee Tower (dp)
- UVALive - 3363 String Compression 区间DP
- UVALive 3683 A Scheduling Problem(树形DP)
- uvalive3608(二分 + DP)
- UVaLive 4256 Salesmen (简单DP)
- UVALive 2038 Strategic game (树形DP,4级)
- UVALive 3363 String Compression (区间DP,4级)
- UVALive 6840 Sweet War (博弈dp)
- UVAlive 6131 dp+斜率优化
- UVALive 5695 -The Last Puzzle -区间dp