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 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

2

4

7

6 4

2 5 10

9 8 12 2

2 12 7

8 2

10

2

1

2 3

1

Sample Output

Case 1: 63

Case 2: 5

//题意第一行是 T 组测试数据,第二行 n 然后是 2*n-1 行的数据

从最上面那里出发,只能选做下走或者右下走,走到最下面那个位置,求路径上最大的和。

//很简单的 dp 注意一些特殊的情况就行

//72ms

 1 #include <stdio.h>
 2 #include <string.h>
 3
 4 int num[210][105];
 5 int dp[210][105];
 6
 7 int max(int a ,int b)
 8 {return  a>b?a:b;}
 9
10 int main()
11 {
12     int T,Case=0;
13     int i,j;
14     scanf("%d",&T);
15     while (T--)
16     {
17         int n;
18         scanf("%d",&n);
19         for (i=1;i<=2*n-1;i++)
20         {
21             if (i<=n)
22                 for (j=1;j<=i;j++)
23                     scanf("%d",&num[i][j]);
24             else
25                 for (j=1;j<=2*n-i;j++)
26                     scanf("%d",&num[i][j]);
27         }
28
29         memset(dp,0,sizeof(dp));
30         for (i=2*n-1;i>=1;i--)
31         {
32             if (i<n)
33             {
34                 for (j=1;j<=i;j++)
35                     dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+num[i][j];
36             }
37             else
38             {
39                 for (j=1;j<=2*n-i;j++)
40                 {
41                     if (j==1)
42                         dp[i][j]=dp[i+1][j]+num[i+1][j];
43                     if (j!=1&&j==2*n-i)
44                         dp[i][j]=dp[i+1][j-1]+num[i][j];
45
46                     dp[i][j]=max(dp[i+1][j-1],dp[i+1][j])+num[i][j];
47                 }
48             }
49         }
50         printf("Case %d: ",++Case);
51         printf("%d\n",dp[1][1]);
52     }
53     return 0;
54 }

时间: 2024-10-08 23:52:45

F - Monkey Banana Problem的相关文章

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 dim

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

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

[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

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 <

[AGC005]:F - Many Easy Problem

F - Many Easy Problems Time limit : 5sec / Memory limit : 512MB Score : 1900 points Problem Statement One day, Takahashi was given the following problem from Aoki: You are given a tree with N vertices and an integer K. The vertices are numbered 1 thr

F - A Simple Problem with Integers(线段树)

Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). "Q a b" 询问[a, b]区间中所有值的和. Input 第一行包含两个整数N, Q.1 ≤ N,Q ≤ 100000. 第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000). 接下来Q行询问,格式如题目描述. Output 对于每一个Q开头的询问

F - A Simple Problem with Integers

Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). "Q a b" 询问[a, b]区间中所有值的和. Input 第一行包含两个整数N, Q.1 ≤ N,Q ≤ 100000. 第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000). 接下来Q行询问,格式如题目描述. Output 对于每一个Q开头的询问