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了. ...
随机推荐
- PostgreSQL设置事务隔离级别实验
apple=# begin; BEGIN apple=# set transaction ISOLATION LEVEL read committed ; SET apple=# select * f ...
- 转:application/json 四种常见的 POST 提交数据方式
四种常见的 POST 提交数据方式 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 PO ...
- Yii 初识
接管一个Yii的系统,因为没有文档,所以非常上火. 01 查版本 Yii::getVersion(); 02 生成webapp Yii 是支持通过命令行生成webapp的.其中, yiic.bat是W ...
- Codeforces 106A:Card Game
题目链接http://codeforces.com/contest/106/problem/A 题意:一套牌有S.H.D.C四种花色,按等级分成6.7.8.9.T.J.Q.K.A.每次选出一个花色作为 ...
- Codeforces 989A:A Blend of Springtime
A. A Blend of Springtime time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.threadpool.ThreadPool
springboot中遇到的, 将guava添加到项目中即可.(当时添加的是guava 18)
- 在Outlook中修改脱机文件(.ost)的保存位置
方法一 少读者所在公司的邮箱客户端都在使用微软 Exchange Server 的“缓存 Exchange 模式”.Outlook会默认将脱机文件(.ost文件)保存在C盘上. 但很多读者不希望Out ...
- [CLPR] 用于加速训练神经网络的二阶方法
本文翻译自: http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi ...
- Fiddler显示响应时间 显示服务器IP
在主界面菜单上 Rules->CustomRules 在class Handlers{}里添加class 如: 显示响应时间 class Handlers { …… ) function Tim ...
- django创建第一个项目helloworld
环境:centos 7,已安装python 3.6环境 1.安装django并创建django第一个项目 1.1.使用pip安装django# pip install Django或指定安装版本# p ...