lightoj 1004 - Monkey Banana Problem 【dp】
2016-04-07 17:53
477 查看
题目链接:lightoj 1004 - Monkey Banana Problem
1004 - Monkey Banana Problem
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
You are in the world of mathematics to solve the great “Monkey Banana Problem”. It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers. Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.
Output
For each case, print the case number and maximum number of bananas eaten by the monkey.
Sample Input
Output for Sample Input
2
4
7
6 4
2 5 10
9 8 12 2
2 12 7
8 2
10
2
1
2 3
1
Case 1: 63
Case 2: 5
Note
Dataset is huge, use faster I/O methods.
题意:给定2*N-1行,要求按照数塔的规定走一条路线使得路线上数之和最大。
思路:我们枚举中间的起点,这样就是双树塔了。注意不合法状态不能转移。
AC代码:
1004 - Monkey Banana Problem
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
You are in the world of mathematics to solve the great “Monkey Banana Problem”. It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers. Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.
Output
For each case, print the case number and maximum number of bananas eaten by the monkey.
Sample Input
Output for Sample Input
2
4
7
6 4
2 5 10
9 8 12 2
2 12 7
8 2
10
2
1
2 3
1
Case 1: 63
Case 2: 5
Note
Dataset is huge, use faster I/O methods.
题意:给定2*N-1行,要求按照数塔的规定走一条路线使得路线上数之和最大。
思路:我们枚举中间的起点,这样就是双树塔了。注意不合法状态不能转移。
AC代码:
//#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #include <stack> #define PI acos(-1.0) #define CLR(a, b) memset(a, (b), sizeof(a)) #define fi first #define se second #define ll o<<1 #define rr o<<1|1 using namespace std; typedef long long LL; typedef pair<int, int> pii; const int MAXN = 1e6 + 10; const int pN = 1e6;// <= 10^7 const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; void add(LL &x, LL y) { x += y; x %= MOD; } LL a[110][110], b[110][110]; LL dp1[110][110], dp2[110][110]; int n; LL Solve(int s) { CLR(dp1, 0); dp1 [s] = a [s]; for(int i = n-1; i >= 1; i-- ){ for(int j = 1; j <= i; j++) { if(dp1[i+1][j+1] && dp1[i+1][j]) { dp1[i][j] = max(dp1[i+1][j+1], dp1[i+1][j]) + a[i][j]; } else if(dp1[i+1][j+1]) { dp1[i][j] = dp1[i+1][j+1] + a[i][j]; } else if(dp1[i+1][j]) { dp1[i][j] = dp1[i+1][j] + a[i][j]; } } } CLR(dp2, 0); dp2 [s] = b [s]; for(int i = n-1; i >= 1; i--) { for(int j = 1; j <= i; j++) { if(dp2[i+1][j+1] && dp2[i+1][j]) { dp2[i][j] = max(dp2[i+1][j+1], dp2[i+1][j]) + b[i][j]; } else if(dp2[i+1][j+1]) { dp2[i][j] = dp2[i+1][j+1] + b[i][j]; } else if(dp2[i+1][j]) { dp2[i][j] = dp2[i+1][j] + b[i][j]; } } } return dp1[1][1] + dp2[1][1] - a [s]; } int main() { int t, kcase = 1; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { scanf("%lld", &a[i][j]); } } for(int i = n; i >= 1; i--) { for(int j = 1; j <= i; j++) { if(i == n) { b[i][j] = a[i][j]; } else { scanf("%lld", &b[i][j]); } } } LL ans = 0; for(int i = 1; i <= n; i++) { ans = max(ans, Solve(i)); } printf("Case %d: %lld\n", kcase++, ans); } return 0; }
相关文章推荐
- 【后缀数组】[POJ 1743]Musical Theme
- android window 一些属性说明
- 79.iOS 设备的UI规范和iOS各控件默认高度
- iOS开发系列--数据存取
- 解压版mysql安装
- 如何解决ajax跨域问题
- linux本地yum源配置
- 从此不求人:自主研发一套PHP前端开发框架(14)
- 教你如何将pdf转换成excel表格格式
- LevelDB学习笔记
- java类加载扩展
- RunLoop应用篇--线程间通信
- 【数位DP】[BZOJ 3876]支线剧情
- 真机调试报错:Please verify that your device’s clock is properly set
- HDU:最短路(单源最短路径Dijistra)
- hibernate
- Ext.grid.CheckboxSelectionModel去除全选框
- 欢迎使用CSDN-markdown编辑器
- static_assert
- 数组01开发日志