Light OJ 1004 - Monkey Banana Problem dp题解

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

三角形塔题目的加强版,这次是上下两个颠倒的三角形叠加一起了。

结果我下标没注意,要调试了很长时间,反复强调下标的精确性了。

方法也是从底往上:只有一条数组的时候,必然是从中选择最大的一个数,有两条数组的时候,那么就是从上一条走到下一条,当前数组的一个格子的数选择可以走到下一个数组的数,这里是两个选择,的最大值加起来。如此从底往上推导,就能出结果了。

不要问我算法有什么用了:

就自娱自乐吧。

小公司是根本用不上的,或者说他们根本没有懂算法的人在里面,也就更加不会有懂算法的人出来当面试官,招懂算法的人了。

算法是屠龙刀,问题是先要找到龙。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

const int MAX_N = 100;
const int MAX_DN = 200;
long long arr[MAX_DN][MAX_N], tbl[MAX_N];

long long getMostBanana(long long A[][MAX_N], long long dp[], int n)
{
	int len = (n<<1)-1;
	memset(dp, 0, sizeof(long long)*n);
	dp[0] = A[len-1][0];
	for (int i = len-2, d = 2; i >= n-1; i--, d++)//注意不是i>=n是i>=n-1
	{
		dp[d-1] = dp[d-2] + A[i][d-1];
		for (int j = d-2; j > 0; j--)
		{
			dp[j] = max(dp[j], dp[j-1]) + A[i][j];
		}
		dp[0] = dp[0] + A[i][0];
	}
	for (int i = n-2, d = n-1; i >= 0; i--, d--)//不能是i=n-1,要i=n-2
	{//一点定的接合不对,就会答案错误。
		for (int j = 0; j < d; j++)
		{
			dp[j] = max(dp[j], dp[j+1]) + A[i][j];
		}
	}
	return dp[0];
}

int main()
{
	int T, n, t = 1;
	scanf("%d", &T);
	while (T--)
	{
		printf("Case %d: ", t++);
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			for (int j = 0; j < i; j++)
				scanf("%lld", &arr[i-1][j]);
		}
		for (int i = n-1, d = n; i >= 1; i--, d++)
		{
			for (int j = 0; j < i; j++)
				scanf("%lld", &arr[d][j]);
		}
		printf("%lld\n", getMostBanana(arr, tbl, n));
	}
	return 0;
}
时间: 2024-12-28 15:13:11

Light OJ 1004 - Monkey Banana Problem dp题解的相关文章

LightOJ 1004 Monkey Banana Problem (DP 数字三角形)

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 "MonkeyBanana Problem". It states that, a monkey enters into a diamond shaped twodimen

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

LightOJ 1004 - Monkey Banana Problem 【DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1004 题意:两个数塔相接,求最大的路径和. 解法:简单DP. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <

(LightOJ 1004) Monkey Banana Problem 简单dp

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). Wh

F - Monkey Banana Problem

F - Monkey Banana Problem Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond s

[LightOJ1004]Monkey Banana Problem(dp)

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1004 题意:数塔的变形,上面一个下面一个,看清楚了直接做就行了. 上半部分转移方程dp(i,j)=max(dp(i-1,j),dp(i-1,j+1))+G(i,j) 下半部分转移方程dp(i,j)=max(dp(i-1,j-1),dp(i-1,j))+G(i,j) 1 #include <algorithm> 2 #include <io

Light OJ 1422 Halloween Costumes 区间DP基础题

Halloween Costumes 题目链接: http://lightoj.com/volume_showproblem.php?problem=1422 题意: Gappu想要去参加一些party,他去每个party都要把特定编号的服装穿在外边,他可以穿上或者脱掉服装(脱掉的服装不能再穿一次,但是可以穿一件相同编号新的服装,最近穿的服装会套在之前穿的服装的外边),问Gappu最少需要准备多少套服装. 题解: 设dp[i][j]为区间 i 到 j (设len为区间长度,j=i+len)内最少

Light OJ 1031 - Easy Game(区间dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅限在一端, 他们的得分分别是取得所有值的和.现在求这两个选手得分差值的最大值. 解题思路:设dp[i][j]代表从i到j这个区间中,所能够得到的最大差值,只需要枚举其中i到j之间的一个数c,作为断电,那么最大值应该为max(sum[c]-sum[i-1]-dp[c+1][j], sum[j]-sum

Light OJ 1030 - Discovering Gold(概率dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的地方, 每个格子中都有价值为v[i]的宝藏. 有一个6面的骰子,数字为从1-6, 每次摇一次骰子, 得到的数字x后, 你可以到达距离当前位置大x的位置, 并且得到那个位置的宝藏. 如果要走的位置在n的外面, 那么在此摇骰子, 直到找到一个合适的数字.到达n位置的时候结束. 现在想知道走到n位置的能够