描述:
输入,行数,之后接数据,第一行一个数据,之后每行加一。
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

思路:
简单动态规划问题。
dp[i][j]定义为到这个数为止(包括这个数)的最大和,则:
dp[i][j] = max(d[i-1][j-1], d[i-1][j]),未考虑边界条件。
则用滚动数组得:

 #include <iostream>
using namespace std; int main()
{
int n, more, ret;
cin >> n;
int* arr = new int[n];
int* new_arr = new int[n]();
bool flag = true; for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) {
if (j == ) {
cin >> more;
if (flag)
new_arr[j] = arr[j] + more;
else
arr[j] = new_arr[j] + more;
} else if (j == i - ) {
cin >> more;
if (flag)
new_arr[j] = arr[j - ] + more;
else
arr[j] = new_arr[j - ] + more;
} else {
cin >> more;
if (flag)
new_arr[j] = max(arr[j - ], arr[j]) + more;
else
arr[j] = max(new_arr[j - ], new_arr[j]) + more;
}
}
flag = !flag;
} ret = arr[];
if (!flag) {
int* d = arr;
delete []d;
arr = new_arr;
}
for (int i = ; i < n; ++i) {
if (ret < arr[i])
ret = arr[i];
}
delete []arr;
cout << ret;
}

再优化下空间:

考虑到每个数的更新,仅和上个数以及前一个数有关,可用一个变量保存前一个数,得:

 #include <iostream>
using namespace std; int main() {
int n, more, ret;
cin >> n;
int* arr = new int[n](); for (int i = ; i <= n; ++i) {
int back = ;
for (int j = ; j < i; ++j) {
if (j == ) {
cin >> more;
back = arr[j];
arr[j] = arr[j] + more;
} else if (j == i - ) {
cin >> more;
arr[j] = back + more;
} else {
cin >> more;
int sa = arr[j];
arr[j] = max(back, arr[j]) + more;
back = sa;
}
}
} ret = arr[];
for (int i = ; i < n; ++i) {
if (ret < arr[i])
ret = arr[i];
}
delete []arr;
cout << ret;
return ;
}

虽然还有些可以优化,但时间和空间复杂度不变,若输入n行,则时间复杂度为O(n^2),空间复杂度为O(n)。

注意由于输入n行,则输入个数为n^2规模,所以n^2是时间复杂度下限。

POJ1163 数学三角求最大路径的更多相关文章

  1. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  2. POJ 1845-Sumdiv【经典数学题目---求因子和】

    转载请注明出处:http://blog.csdn.net/lyy289065406/article/details/6648539 優YoU  http://user.qzone.qq.com/289 ...

  3. HDU 3861--The King’s Problem【scc缩点构图 &amp;&amp; 二分匹配求最小路径覆盖】

    The King's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU 5105 Math Problem --数学,求导

    官方题解: f(x)=|a∗x3+b∗x2+c∗x+d|, 求最大值.令g(x)=a∗x3+b∗x2+c∗x+d,f(x)的最大值即为g(x)的正最大值,或者是负最小值.a!=0时, g′(x)=3∗ ...

  5. <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字

    1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...

  6. MT【51】一道三角求最值问题

    [Genius is one percent inspiration and ninety-nine percent perspiration]--- 爱迪生 [Without the one per ...

  7. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

  8. POJ - 3984 迷宫问题 BFS求具体路径坐标

    迷宫问题 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...

  9. UVALive - 7831 :ACM Tax (主席树求树路径上中位数:LCA+主席树)

    题意:给定一棵带权树,Q次询问,每次询问路径上的中位数. 思路:中位数分边数奇偶考虑,当当边数为num=奇时,结果就算路径第num/2+1大,用主席树做即可... (做了几道比较难的主席树,都wa了. ...

随机推荐

  1. Jmeter-Transaction Controller(事务控制器)

    generate parent sample:生成父样本 include duration of timer and pre-post processors in generated sample:在 ...

  2. LOJ10131. 「一本通 4.4 例 2」暗的连锁【树上差分】

    LINK solution 很简单的题 你就考虑实际上是对每一个边求出两端节点分别在两个子树里面的附加边的数量 然后这个值是0第二次随便切有m种方案,如果这个值是1第二次只有一种方案 如果这个值是2或 ...

  3. test20181024 zi

    题意 分析 这种题一般是推公式,发现必须求得的量,然后定义函数记忆化. 然后那些函数里面又是递归处理,合并. 代码 为了不爆空间,用map存记忆化内容. #include<bits/stdc++ ...

  4. vue.js权威指南----代码解释实例

    1:P61(值绑定) <input type="checkbox" v-model="toggle" :true-value="a" ...

  5. HTML表单 在提交之前 验证表单数字合法性

    function checkform(){ if(!isNumeric($('.apply_money').val())){ alert("必须是数字"); return fals ...

  6. AS3 判断双击事件

    //双击事件触发的时候不触发单击事件 package { import com.greensock.TweenLite; import flash.display.DisplayObjectConta ...

  7. python random模块(随机数)详解

    使用前要先导入random模块 import random random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random ...

  8. github之本地上传

    在打算上传到github之前需要在github上面首先创建一个项目(点击右上角“+”号,点击New repository):

  9. WCF 快速入门

    定义服务契约 构建HelloWCF应用的第一步是创建服务契约.契约式是表示消息应用外形的主要方式.对于外形,是指服务暴露的操作,使用的消息 schema和每个操作实现的消息交换模式(MEP).总之,契 ...

  10. Java 获取字符串长度 length()

    Java 手册 实例: public class Length { public static void main(String[] args) { String str = "hgdfas ...