poj 2479 Maximum sum(递推)
题意:给定n个数,求两段连续不重叠子段的最大和。
思路非常easy。把原串划为两段。求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n到i的最大连续子串和,这样将时间复杂度降为O(n)。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std; const int maxn = 50000 + 50;
const int INF = 0x3f3f3f3f;
int n, a[maxn], lmax[maxn], rmax[maxn]; void init() {
cin >> n;
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
int enda = a[1];
lmax[1] = a[1];
for(int i = 2; i <= n; i++) {
enda = max(enda+a[i], a[i]);
lmax[i] = max(lmax[i-1], enda);
}
enda = a[n];
rmax[n] = a[n];
for(int i = n-1; i >= 1; i--) {
enda = max(enda+a[i], a[i]);
rmax[i] = max(rmax[i+1], enda);
}
} void solve() {
int ans = -INF;
for(int i = 1; i < n; i++) ans = max(ans, lmax[i]+rmax[i+1]);
cout << ans << endl;
} int main() {
//freopen("input.txt", "r", stdin);
int t; cin >> t;
while(t--) {
init();
solve();
}
return 0;
}
poj 2479 Maximum sum(递推)的更多相关文章
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
- (线性dp 最大连续和)POJ 2479 Maximum sum
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 44459 Accepted: 13794 Des ...
- POJ 2479 Maximum sum(双向DP)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36100 Accepted: 11213 Des ...
- poj 2479 Maximum sum (最大字段和的变形)
题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...
- 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 #2479 - Maximum sum
Hi, I'm back. This is a realy classic DP problem to code. 1. You have to be crystal clear about what ...
- [poj 2479] Maximum sum -- 转载
转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410 ...
- POJ 1664 放苹果 (递推)
题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...
随机推荐
- JAVA与VB通过SOCKET通讯
JAVA与VB通过SOCKET通讯 在做项目的过程中,本来是想使用JAVA的comm.jar工具实现串口通讯,不知道怎么回事,总是取不到电脑的串口.所以,改为现在的这种模式:java通过socket给 ...
- HDFS设计思想
HDFS设计思想 DataNode:用来在磁盘上存储数据 HDFS 数据存储单元( block ) 1 文件被切分成固定大小的数据block块 •默认数据块大小为 64MB(hadoop1.x版本6 ...
- css 两列布局中单列定宽单列自适应布局的6种思路
前面的话 说起自适应布局方式,单列定宽单列自适应布局是最基本的布局形式.本文将从float.inline-block.table.absolute.flex和grid这六种思路来详细说明如何巧妙地实现 ...
- Mongoose 参考手册
转自:https://cnodejs.org/topic/548e54d157fd3ae46b233502 Mongoose 是什么? 一般我们不直接用MongoDB的函数来操作MongoDB数据库 ...
- div模拟dropdownlist控件 div下拉菜单
原文发布时间为:2009-10-16 -- 来源于本人的百度文章 [由搬家工具导入] 控件发布:div2dropdownlist(div模拟dropdownlist控件) div3dropdownli ...
- 页面get post等查看
原文发布时间为:2010-03-08 -- 来源于本人的百度文章 [由搬家工具导入] http://www.fiddler2.com/Fiddler2/firstrun.asp
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---4
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: <Linux命令行与shell脚本 ...
- c# Thread类
现在C#已经建议摈弃使用 Suspend, Resume 暂停/恢复线程, 也尽量少用 Abort方法中断一个线程. 建议使用线程的同步手段有: Mutex.ManualResetEvent.Aut ...
- hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
hdu3879 base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...
- HDU 1241.Oil Deposits-求连通块DFS or BFS
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...