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 ...
随机推荐
- no for & 100 Array & Uint8Array & Typed Arrays
no for & 100 Array http://hiluluke.cn/ bad function generate100Array() { var arr = new Array(100 ...
- [LOJ#2324]「清华集训 2017」小Y和二叉树
[LOJ#2324]「清华集训 2017」小Y和二叉树 试题描述 小Y是一个心灵手巧的OIer,她有许多二叉树模型. 小Y的二叉树模型中,每个结点都具有一个编号,小Y把她最喜欢的一个二叉树模型挂在了墙 ...
- 排列计数(permutation)
排列计数(permutation) 题目描述 求有多少种长度为n的序列A,满足以下条件: 1) 1~n这n个数在序列中各出现了一次 2) 若第i个数A[i]的值为i,则称i是稳定的.序列恰好有m个数是 ...
- [AGC002D] Stamp Rally (并查集+整体二分)
Description 给你一个n个点m个条边构成的简单无向连通图,有Q组询问,每次询问从两个点x,y走出两条路径,使这两条路径覆盖z个点,求得一种方案使得路径上经过的变的最大编号最小. Input ...
- WebService 序列化和反序列化
参考了Fish LI的Xml读取文章,写了XML序列化和反序列化的文章. 序列化:把实体列转化成XML.反序列化:把XML按一定的规则转化成需要的实体列. 序列化和反序列化化使用到的类, using ...
- js判断手机的左右滑动
js代码 $(function() { function judge() { var startx;//让startx在touch事件函数里是全局性变量. var endx; var el = doc ...
- 浮点优化选项 -ffast-math:极大地提高浮点运算速度【转】
转自:http://blog.csdn.net/zjujoe/article/details/2604157 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 关于浮点优化选项摘 ...
- PHPCMS V9 采集实例
采集目标: 列表: http://www.cnys.com/zixun/list_2_2.html
- 学习环境配置:Manjaro、MSYS2以及常见软件
0.前言 在说Manjaro之前,要先说一下Linux发行版.对于各大发行版而言,内核只有版本的差异,最重要的区别就是包管理系统.常见的包管理系统包括:Pacman,Apt , Yum和Portage ...
- ZOJ 3593.One Person Game-扩展欧几里得(exgcd)
智障了,智障了,水一水博客. 本来是个水题,但是for循环遍历那里写挫了... One Person Game Time Limit: 2 Seconds Memory Limit: 655 ...