POJ 2479 不相交最大子段和
题目意思还是很好理解的,在一个数列中,找出不相交的两个子串使得其和最大。
解题思路:
对于每个i来说,求出[0 ~ i - 1] 的最大子段和以及[i ~ n - 1]的最大子段和,在加起来,求最大的一个就行了。
[0 ~ i - 1]的最大子段和从左向右扫描,[i ~ n - 1] 的最大子段和从右向左扫描即可。时间复杂度为 O(n)
source code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x))) const int INF = 0x3f3f3f3f;
int a[], left[], right[];
int main(){
int i, j, t, k, n, m;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i = ; i < n; ++i) scanf("%d",&a[i]);
left[] = a[];
for(i = ; i < n; ++i){
if(left[i - ] < )
left[i] = a[i];
else
left[i] = left[i - ] + a[i];
}
for(i = ; i < n; ++i){
left[i] = Max(left[i - ], left[i]); //Max segment sum
}
right[n - ] = a[n - ];
for(i = n - ; i >= ; --i){
if(right[i + ] < )
right[i] = a[i];
else
right[i] = right[i + ] + a[i];
}
for(i = n - ; i > ; --i){
right[i] = Max(right[i + ], right[i]); //Max segment sum
}
int MAX = -INF;
for(i = ; i < n; ++i){
MAX = Max(MAX, left[i - ] + right[i]);
}
printf("%d\n",MAX);
}
return ;
}
POJ 2479 不相交最大子段和的更多相关文章
- 动态规划(DP),递推,最大子段和,POJ(2479,2593)
题目链接:http://poj.org/problem?id=2479 解题报告: 1.再求left[i]的时候,先没有考虑a[i]的正负,先把a[i]放到left[i]中,然后left=max(le ...
- poj----Maximum sum(poj 2479)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30704 Accepted: 9408 Desc ...
- POJ 2479
---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...
- poj 2479 (DP)
求一个区间内连续两段不相交区间最大和. // File Name: 2479.cpp // Author: Missa_Chen // Created Time: 2013年06月22日 星期六 16 ...
- poj 2479 Maximum sum (最大字段和的变形)
题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- Maximum sum(poj 2479)
题意:给一段数列,将这个数列分成两部分,使两部分的最大子段和的和最大,输出和 /* 看数据没想到是(O)n的算法,求出从前向后的最大子段和和从后向前的最大子段和, 然后枚举断点. 第一次提交不小心折在 ...
- To the Max POJ - 1050 (最大子段和)
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
随机推荐
- [LeetCode]题解(python):126-Word Ladder II
题目来源: https://leetcode.com/problems/word-ladder-ii/ 题意分析: 给定一个beginWord和一个endWord,以及一个字典单词,找出所有从begi ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
原文 (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windo ...
- ALV的html表头
在ALV的function的exporting里添加属性: I_CALLBACK_HTML_TOP_OF_PAGE = alv_top_of_page 定义form响应上述ALV属性 *&-- ...
- PHP第一章学习——了解PHP(下)
继续昨天的部分! —————————————————————————————— 首先Ubuntu下安装Apache软件: ubuntu更新源有问题,又要解决半天! 我现在很冷静! 安装Apache教程 ...
- android service和activity的通讯
我们须要用下面方式来启动service: </pre><pre name="code" class="java"><span st ...
- 条款05:了解C++默默编写并调用哪些函数
每一个class都会有一个或多个构造函数.一个析构函数.一个copy assignment操作符.这些控制着基础操作,像是产出新对象并确保它被初始化.摆脱旧对象并确保它被适当清理.以及赋予对象新值. ...
- debian6 更新python版本到python3.3
1.下载python3.3安装包 #wget wget --no-cookie --no-check-certificate --header "Cookie:gpw_e24=http%3A ...
- thinkphp第二天
1.使用print_r();打印数组的时候最好使用<pre>标签,可以是数组表现的更加直观. pre 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本 ...
- yii配置
一.在config/web.php中添加如下代码(开启debug工具和gii工具) if (YII_ENV_DEV) { // configuration adjustments for 'dev' ...