Max Sequence
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16850   Accepted: 7054

Description

Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N).


You should output S.

Input

The
input will consist of several test cases. For each test case, one
integer N (2 <= N <= 100000) is given in the first line. Second
line contains N integers. The input is terminated by a single line with N
= 0.

Output

For each test of the input, print a line containing S.

Sample Input

5
-5 9 -5 11 20
0

Sample Output

40

Source

求解序列中两段不相交的子序列的最大和。
先正着扫描 1- n-1 区间求出每个段的最大值,然后反着扫描 n - 2这段区间求出每个段的最大值,然后枚举1 - n-1 每个段,得到最大值

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = ; int a[N];
int dp[N],dp1[N]; ///dp[i]第 1-i段的最大和 , dp1[i] 第 i - n段的最大和
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int sum=,mx = -; ///每个数都有可能是负数
for(int i=;i<n;i++){ ///因为题目中两段不能重合,所以不能枚举到n
sum +=a[i];
if(sum>mx) mx = sum;
if(sum<){
sum = ;
}
dp[i]=mx;
}
//for(int i=1;i<=n;i++) printf("%d ",dp[i]);
sum = ,mx = -;
for(int i=n;i>;i--) ///因为题目中两段不能重合,所以不能枚举到1
{
sum+=a[i];
if(sum>mx) mx = sum;
if(sum<) sum = ;
dp1[i] =mx;
}
//for(int i=1;i<=n;i++) printf("%d ",dp1[i]);
mx = -;
for(int i=;i<n;i++){
if(dp[i]+dp1[i+]>mx){
mx = dp[i]+dp1[i+];
}
}
printf("%d\n",mx);
}
return ;
}

poj 2593&&poj2479(最大两子段和)的更多相关文章

  1. Poj2479 & Poj 2593

    就是按着DP的思路来做的,结果还是想不到.T_T,行了,别玻璃心了,继续. 这道题目是求在一列数里,由两部分子段和组成的最大和.即对于连续整数组成的串 S1.S2,使 S1 + S2 的和最大. 题目 ...

  2. poj 2593 Max Sequence(线性dp)

    题目链接:http://poj.org/problem?id=2593 思路分析:该问题为求给定由N个整数组成的序列,要求确定序列A的2个不相交子段,使这m个子段的最大连续子段和的和最大. 该问题与p ...

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

  4. poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数

    Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 379 ...

  5. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

  6. Frogger POJ - 2253(求两个石头之间”所有通路中最长边中“的最小边)

    题意 ​ 题目主要说的是,有两只青蛙,在两个石头上,他们之间也有一些石头,一只青蛙要想到达另一只青蛙所在地方,必须跳在石头上.题目中给出了两只青蛙的初始位置,以及剩余石头的位置,问一只青蛙到达另一只青 ...

  7. POJ 2593&&2479:Max Sequence

    Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16329   Accepted: 6848 Des ...

  8. POJ 2593 Max Sequence

    Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17678   Accepted: 7401 Des ...

  9. 【POJ 1269】判断两直线相交

    题 利用叉积解方程 #include <cstdio> #define MAX 1<<31 #define dd double int xmult(dd x1,dd y1,dd ...

随机推荐

  1. SpringBoot-配置文件属性注入-3种方式

    配置文件: datasource.username = admin datasource.url = /hello/world 方式一: @Value 前提: <!-- JavaBean处理工具 ...

  2. 从零开始学Linux系统(四)之Vi/Vim操作指令

    模式切换: 编辑模式 <-- [:]<--命令模式 -->[a.i.o A.I.O]-->  插入模式 编辑模式操作: 设置行号  :set nu   :set nonu 复制 ...

  3. Emmet缩写语法

    缩写语法 Emmet 使用类似于 CSS 选择器的语法描述元素在生成的文档树中的位置及其属性. 元素 可以使用元素名(如 div 或者 p)来生成 HTML 标签.Emmet 没有预定义的有效元素名的 ...

  4. Ubuntu中python多版本管理工具-pyenv

    ubuntu系统版本:16.04 # lsb_release -aNo LSB modules are available.Distributor ID: UbuntuDescription: Ubu ...

  5. ACE线程管理机制-线程的创建与管理

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/04/581369.html 有过在不同的操作系统下用c++进行过多线程编程的朋友对那些线程处理 ...

  6. Codeforces Round #401 (Div. 2) A B C 水 贪心 dp

    A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...

  7. HDU 5656

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  8. JS学习之数组

  9. sublime text3常用快捷键

    Ctrl+L 选择整行(按住-继续选择下行) Ctrl+KK 从光标处删除至行尾 Ctrl+Shift+K 删除整行 Ctrl+Shift+D 复制光标所在整行,插入在该行之前 Ctrl+J 合并行( ...

  10. Maven命令创建java项目

    ------------------------------java项目搭建--------------------------- 使用Maven构建一个简单的Java项目 1.进入命令行,执行下面的 ...