POJ1163 数学三角求最大路径
描述:
输入,行数,之后接数据,第一行一个数据,之后每行加一。
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 数学三角求最大路径的更多相关文章
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- POJ 1845-Sumdiv【经典数学题目---求因子和】
转载请注明出处:http://blog.csdn.net/lyy289065406/article/details/6648539 優YoU http://user.qzone.qq.com/289 ...
- HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】
The King's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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∗ ...
- <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字
1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...
- MT【51】一道三角求最值问题
[Genius is one percent inspiration and ninety-nine percent perspiration]--- 爱迪生 [Without the one per ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
- 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, ...
- UVALive - 7831 :ACM Tax (主席树求树路径上中位数:LCA+主席树)
题意:给定一棵带权树,Q次询问,每次询问路径上的中位数. 思路:中位数分边数奇偶考虑,当当边数为num=奇时,结果就算路径第num/2+1大,用主席树做即可... (做了几道比较难的主席树,都wa了. ...
随机推荐
- OPENCV Linux安装
https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
- tensorflow :ckpt模型转换为pytorch : hdf5模型
参考链接:https://github.com/bermanmaxim/jaccardSegment/blob/master/ckpt_to_dd.py import tensorflow as tf ...
- ES6中箭头函数的作用
我们知道在ES6中,引入了箭头函数,其本质就是等同有ES5中的函数.类似于下面的写法: let test1=() => “abc”; let test2=() => { return “a ...
- sublime text2下配置c++
今天安装了sublime text2,真是编辑神器,不再用notepad了. 笔记本上没有c++运行环境,用编辑器既轻巧,又方便,VS太臃肿了. 要在sublime text2 下运行c++程序,需要 ...
- 老爷机iphone4s 9.2.1降级6.1.3
原帖见威锋网 sunnyskyline 2017年1月10日发的贴. 本文中加了一些我自己的情况,也是一知半解,抛砖引玉吧. 首先进行备份.进行备份.进行备份. 感谢大神@极端阴险 感谢@shuaig ...
- addFrameScript用法
如何判断一个mc播放完毕之后remove掉他呢, 目前我发现的两种做法,一种是: var boomMc:MovieClip = LoadResource.getMC("boom", ...
- 微信后端服务架构及其过载控制系统DAGOR
微信架构介绍 眼下的微信后端包含3000多个移动服务,包括即时消息.社交网络.移动支付和第三方授权.该平台每天收到的外部请求在10 ^10个至10^11个.每个这样的请求都会触发多得多的内部微服务 ...
- oracle之 RAC 11G ASM下控制文件多路复用
如果数据库仅有一组control file文件,需要添加一组或者多组,保证一组文件损坏或者丢失导致数据库宕机. -- 环境说明SQL> select * from v$version;BANNE ...
- POJ2226Muddy Fields
题目:http://poj.org/problem?id=2226 巧妙建图:以行或列上的联通块作为点,每个泥格子作为边,求最小点覆盖就可以了! 于是用匈牙利算法找最大匹配.注意要对右部点记录每一个左 ...
- 解决 eclipse tomcat cannot create a server using the selected type
解决的方法 1.退出eclipse: 2.打开 [工程目录下]/.metadata/.plugins/org.eclipse.core.runtime/.settings目录: 3.删除org.ecl ...