Light oj-1004 - Monkey Banana Problem,数字三角形的变形版~
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.
说白了这道题就是两个数字三角形,一个正的,一个倒过来的;倒着的做法与数字三角形一样,只需要处理正的;
我们来看样例,把这个分为上下两部分,下面就是个倒的数字三角形,正的三角形其实与倒的处理方式类似,我们做数字三角形是从下往上推,或者从上往下推,如果两种都会的话这道题没什么问题了;
博主其实做这个以前都是从下往上推的,但这题分为两部分就不好处理了,所以我选择正三角形从上往下推,而倒三角形相当于一个正的三角形从下往上推,做法不就和纯的数字三角形一模一样了;若不懂来看代码;
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
//const int INF=0x3f3f3f3f;
const int N=200+10;//开二维数组足够了,没有用到空间压缩;
long long a[N][N];
int main()
{
int t,n,i,j;
memset(a,0,sizeof(a));
scanf("%d",&t);
int t1=t;
while(t--)
{
scanf("%d",&n);
for(i=1; i<=2*n-1; i++)
{
if(i<=n)
{
for(j=1; j<=i; j++)
scanf("%lld",&a[i][j]);//正三角形;
}
else
{
for(j=1; j<=2*n-i; j++)//倒三角;
scanf("%lld",&a[i][j]);
}
}
printf("Case %d: ",t1-t);
for(i=2; i<=n; i++)
for(j=1; j<=i; j++)
a[i][j]+=max(a[i-1][j-1],a[i-1][j]);//从上往下,状态转移方程;
for(i=n+1; i<=2*n-1; i++)
for(j=1; j<=2*n-i; j++)
a[i][j]+=max(a[i-1][j],a[i-1][j+1]);//状态转移方程几乎一样
printf("%lld\n",a[2*n-1][1]);注意格式,博主用I64dPEI
}
return 0;
}
Light oj-1004 - Monkey Banana Problem,数字三角形的变形版~的更多相关文章
- Light OJ 1004 - Monkey Banana Problem(DP)
题目大意: 给你一菱形的数字阵,问从最上面走到最下面所能获得的最大值是多少? #include<cstdio> #include<cstring> #include<io ...
- Lightoj 1004 - Monkey Banana Problem
题目链接:http://acm.hust.edu.cn/vjudge/contest/121396#problem/F http://lightoj.com/volume_showproblem.ph ...
- Monkey Banana Problem LightOJ - 1004
Monkey Banana Problem LightOJ - 1004 错误记录: 1.数组开小2.每组数据数组没有清空 #include<cstdio> #include<cst ...
- (LightOJ 1004) Monkey Banana Problem 简单dp
You are in the world of mathematics to solve the great "Monkey Banana Problem". It states ...
- F - Monkey Banana Problem
F - Monkey Banana Problem Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
- 2016huasacm暑假集训训练五 F - Monkey Banana Problem
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...
- [LightOJ1004]Monkey Banana Problem(dp)
题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1004 题意:数塔的变形,上面一个下面一个,看清楚 ...
- Light OJ Dynamic Programming
免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一 ...
- HDU 1176 免费馅饼 (类似数字三角形的题,很经典,值得仔细理解的dp思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- 51nod 1116 K进制下的大数
你万万想不到,Long Long 就能存下的数据 #include <iostream> #include <cstdio> #include <cstdlib> ...
- Tree CodeForces -932D
错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...
- Spring中bean的五个作用域简介(转载)
Spring上个版本的IoC容器支持两个不同的bean作用域(单例与原型).Spring 2.0改进了这一点,不仅提供了一些依赖于Spring部署环境(比如说,在web环境中的request和sess ...
- Coprime Conundrum 容斥原理
https://www.hackerrank.com/contests/hourrank-13/challenges/arthur-and-coprimes 我们可以枚举每一个p在[2, sqrt(n ...
- poj3233Matrix Power Series
链接 也是矩阵经典题目 二分递归求解 a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)( ...
- 配置Oracle监听器
Oracle的监听和网络服务都可以在Net Manager中配置,如下图.也可以在上面的那个Net Configuration Assistant中配置,只是Net Manager比较方便些. Ora ...
- C#代码规范(简版)
C#项目代码规范 目的 1.方便代码的交流和维护. 2.不影响编码的效率,不与大众习惯冲突. 3.使代码更美观.阅读更方便. 4.使代码的逻辑更清晰.更易于理解. 在C#中通常使用的两种编码方式如下 ...
- Glide清除缓存
Glide是谷歌推荐的一款加载图片的第三方框架,对内存优化更好,更省资源,他的众多好处,我就不一一描述了,有兴趣的朋友可以百度一下,介绍的还是挺详细的. 今天主要给大家介绍一下关于怎么获取Glide的 ...
- java中字节和字符的转换操作
package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputSt ...
- REMOVE A WINDOWS SERVICE
You can easily remove a Windows service from the Windows registry using a simple command prompt comm ...