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了. ...
随机推荐
- spring注解-@Autowired。@Resource。@Service
Spring的@Autowired注解.@Resource注解和@Service注解 什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: ...
- try...except语句
try: 执行语句 except 执行语句有异常就执行这一步 else: 执行语句没有异常就执行这一步 finally 不管有没有异常,这一步就要执行
- Ambiguous reference to member 'dataTask(with:completionHandle:)'错误
在研究IOS的网络请求过程中,因为NSURLConnection已经过时,需要引用到URLSession var url:NSURL=NSURL(string: "http://3g.163 ...
- ssh框架中spring整合hibernate的配置文件模板(带详细注释)
applicationContext.xml的配置文件模板 <?xml version="1.0" encoding="UTF-8"?> <b ...
- Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录
此为在网络上找来的,觉得很好! 实现目的: 对Mongodb数据库日志按天保存,并且只保留最近7天的日志记录. 具体操作: 使用Mongodb数据库自带的命令来切割日志 ps -def | grep ...
- Appium Desktop介绍-xcodebuild failed with code 65 问题解决
Appium Desktop介绍-xcodebuild failed with code 65 问题解决 一.Appium Desktop介绍 Appium Desktop是一款用于Mac.Wind ...
- thinkphp3.2.3+smarty解决success调用模板错误心得
最近学习thinkphp上瘾,出现success找不到模板问题,查阅各大神解决方案,分享一下针对新手如何解决该问题,如有不对的地方请大神指正 1.首先修改自己的config文件,添加如下配置代码:// ...
- OpenSSL生成root CA及签发证书
一.openssl 简介 openssl 是目前最流行的 SSL 密码库工具,其提供了一个通用.健壮.功能完备的工具套件,用以支持SSL/TLS 协议的实现.官网:https://www.openss ...
- 慕课网 -- 性能优化之PHP优化总结笔记
视频链接,感兴趣的可以去看看,对我来说耳目一新. http://www.imooc.com/learn/205 什么情况下遇到PHP性能问题 1 :PHP语法使用不恰当 2 :使用了PHP语言他不擅长 ...
- Mongodb第一步资料
学习的时候收集了部分好文章,这个文章主要收集的是下载,安装的基本资料 官方http://docs.mongodb.org/manual/tutorial/install-mongodb-on-wind ...