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. POI 2018.10.20

    [POI2005]BANK-Cash Dispenser 有多少个4位字符串是所有操作序列的子串. 10^4枚举字符串.暴力判断会TLE 发现,我们就是在每个操作序列中不断找第一个出现的c字符. 预处 ...

  2. Android源码4.4.4_r1下载和编译

    系统:ubuntu 16.04.2 TLS 1.源码下载: sudo apt-get install curl curl https://storage.googleapis.com/git-repo ...

  3. vector去除重复的元素

    vector<int> v; sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end());

  4. JavaScript转换与解析JSON的方法

    在JavaScript中将JSON的字符串解析成JSON数据格式,一般有两种方式: 一种为使用eval()函数. 使用Function对象来进行返回解析. 使用eval函数来解析,jquery的eac ...

  5. JavaScript中Unicode值转字符

    在JavaScript中,将Unicode值转字符的方法: <!DOCTYPE html> <html> <head> <meta charset=" ...

  6. iphone6 iPhone6 Plus的导航栏等高度

    iPhone6                                                                                iPhone6 Plus ...

  7. BIRT-商务智能报表工具开发案例指南

    BIRT 报表 脚本 开发 数据库 http://www.iteye.com/topic/1128694 打算近期出版一本全面介绍BIRT使用的书籍,能够帮助大家全面了解BIRT的方方面面,用丰富的案 ...

  8. 插入排序Insertion sort 2

    原理类似桶排序,这里总是需要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,暂时忽视十位数 例如 待排序数组[62,14,59,88,16]简单点五个数字 分 ...

  9. iOS 隐藏导航栏下的黑线

    一.找到导航栏下的黑线 // 寻找导航栏下的黑线 - (UIImageView *)findHairlineImageViewUnder:(UIView *)view { if ([view isKi ...

  10. mysql 替换

    最近贷后好烦,经常让我修改短信模板内容,以前一两个模板手动就直接改了.随着短信模板的增多,手动一个个改内容就不行了. 今天又让我把短信模板中所有的的电话号码修改一下:如:010-44444444改为0 ...