poj 2593 Max Sequence(线性dp)
题目链接:http://poj.org/problem?id=2593
思路分析:该问题为求给定由N个整数组成的序列,要求确定序列A的2个不相交子段,使这m个子段的最大连续子段和的和最大。
该问题与poj 2479相同,解法也一样;
代码如下:
#include <cstdio>
#include <iostream>
using namespace std; const int MAX_N = + ;
int arr[MAX_N], dp_s[MAX_N], dp_r[MAX_N];
int arr_num; int main(int argc, char *argv[])
{
int temp_ans, sum, ans; while (scanf("%d", &arr_num) != EOF && arr_num != )
{
for (int i = ; i < arr_num; ++i)
scanf("%d", &arr[i]); temp_ans = INT_MIN, sum = ;
for (int i = ; i < arr_num; ++i)
{
if (sum >= )
sum += arr[i];
else
sum = arr[i];
if (sum > temp_ans)
temp_ans = sum;
dp_s[i] = temp_ans;
} temp_ans = INT_MIN, sum = ;
for (int i = arr_num - ; i >= ; --i)
{
if (sum >= )
sum += arr[i];
else
sum = arr[i];
if (sum > temp_ans)
temp_ans = sum;
dp_r[i] = temp_ans;
} ans = dp_s[] + dp_r[];
for (int i = ; i < arr_num - ; ++i)
{
int sum = dp_s[i] + dp_r[i + ];
if (sum > ans)
ans = sum;
}
printf("%d\n", ans);
} return ;
}
poj 2593 Max Sequence(线性dp)的更多相关文章
- POJ 2479 Maximum sum POJ 2593 Max Sequence
d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...
- POJ 2593 Max Sequence
Max Sequence Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17678 Accepted: 7401 Des ...
- POJ 1458-Common Subsequence(线性dp/LCS)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39009 Accepted: 15 ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- poj 1836 Alignment(线性dp)
题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...
- poj 1141 Brackets Sequence 区间dp,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- poj 1141 Brackets Sequence ( 区间dp+输出方案 )
http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...
- HDOJ 1003 Max Sum(线性dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 思路分析:该问题为最大连续子段和问题,使用动态规划求解: 1)最优子结构:假设数组为A[0, 1 ...
随机推荐
- C++_知识点_全局变量
全局变量 -全局变量即在函数之外定义的变量 -全局变量保存在静态存储区 注意: -全局变量只能声明和初始化 -全局变量不能进行运算.赋值(非初始化).调用函数 -否则会出现编译错误 -error: e ...
- 笔记-AndroidStudio开发环境的搭建
首先当然是下载AndroidStudio,目前最新的稳定版是1.1 然后下载studio版本的sdk,如果用原装sdk,需要更新 安装的过程中会选择sdk的路径,此时如果已经解压了原装sdk,会进 ...
- C++ this 指针
类的(非静态)成员函数具有一个附加的隐含形参,即指向该类对象的一个指针.这个隐含形参命名为this,与调用成员函数的对象绑定在一起.成员函数不能定义this形参,而是由编译器隐含地定义.成员函数的函数 ...
- js浮点数精度问题
大多数语言在处理浮点数的时候都会遇到精度问题,但是在JS里似乎特别严重,来看一个例子 alert(45.6*13); 结果居然是592.800000000001,当然加法之类的也会有这个问题 那这是j ...
- Web颜色搭配 - 收集
颜色1 颜色一 背景 字 RGB 43,41,46 92,187,207 HEX #2B292E #5CBBCF HSB 264,11,18 190,56,81 CMYK 7,11,0,82 5 ...
- poj 3348 Cows 求凸包面积
题目链接 大意: 求凸包的面积. #include <iostream> #include <vector> #include <cstdio> #include ...
- Windows Azure 现已完全受 Juju 支持
我们很高兴地宣布,Windows Azure 现已完全受 Juju 支持,这也是我们为实现开放性和互操作性而不断努力的结果.这意味着 Ubuntu 用户现在可以使用 Juju 及其直观的图形用户界面设 ...
- c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow
c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow fcgio.cpp:50: error ...
- ListBox控件的操作与实现
.NET FrameWork>參考>类库>System.Windows.Forms>ListBox类的属性 1. 属性列表: SelectionMode 组件中 ...
- jquery表格可编辑修改表格里面的值,点击td变input无刷新更新表格
td点击后变为input可以输入,更新数据,无刷新更新 演示 XML/HTML Code <table border="0" cellpadding="0" ...