题意:给定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(递推)的更多相关文章

  1. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

  2. (线性dp 最大连续和)POJ 2479 Maximum sum

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44459   Accepted: 13794 Des ...

  3. POJ 2479 Maximum sum(双向DP)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Des ...

  4. poj 2479 Maximum sum (最大字段和的变形)

    题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...

  5. 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} 即求两个子序列和的和的最大 ...

  6. 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 ...

  7. [poj 2479] Maximum sum -- 转载

    转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                         ...

  8. POJ 1664 放苹果 (递推)

    题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] ...

  9. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

随机推荐

  1. 洛谷9月月赛round2

    洛谷9月月赛2 t1 题意:懒得说了 分析:模拟 代码: program flag; var a:..,..]of char; n,i,m,j,x,y,ans,k:longint; begin ass ...

  2. NetScaler通过DHCP服务器获取IP地址

    NetScaler通过DHCP服务器获取IP地址 DHCP 选项参考 https://www.iana.org/assignments/bootp-dhcp-parameters/bootp-dhcp ...

  3. AtCoder Regular Contest 092 B Two Sequences

    题目大意 给定两个长为 $n$ 个整数序列 $a_1, \dots, a_n$ 和 $b_1, \dots, b_n$ .求所有 $a_i + b_j$($1\le i, j\le n$)的 XOR ...

  4. 核苷酸(evolution)

    核苷酸(evolution) 题目描述 生物课是帕特里克最讨厌的课程,没有之一. 相比做一些无聊而又无趣的遗传题,他更喜欢其他所有的科目. 包括英语. 但是今天不同.他被一个关于RNA感染DNA的题目 ...

  5. 满汉全席(banquet)

    满汉全席(banquet) 题目描述 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做 ...

  6. 【bzoj2946】[Poi2000]公共串 后缀自动机

    [Poi2000]公共串 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1386  Solved: 620[Submit][Status][Discus ...

  7. python 小练习1

    _input = ['I',6,6,'love','python',6] _str = '' _sum = 0 for item in _input: if isinstance(item,str): ...

  8. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  9. JavaScript (JS)基础:BOM 浅析 (含window对象相关基本方法、属性解析)

    ① window对象(Math方法也属于window对象): window对象是JavaScript中的顶级对象,所有定义在全局作用域中的变量.函数都会变成window对象的属性和方法,window对 ...

  10. wsl折腾记

    参考1 wsl在哪 C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndg ...