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. webpack优化总结

    1. 分包 将不需要变动的第三方包分离出去, 主要方法有: (1). externals(2). DllPlugin(3). expose-loader(4). ProviderPlugin 2. 拆 ...

  2. 20181015 考试记录&数论

    题目传送门 W神爷的题解 数论 小 M 的算式 [问题描述] 小 M 在做数学作业的时候遇到了一个有趣的问题:有一个长度为 n 的数字 串 S,小 M 需要在数字之间填入若干个“+”和恰好一个“=”, ...

  3. java 实现多个接口 方法重名的解决办法——内部类

    package com.kk.innerClass; /** * 通过内部类实现接口 * 解决多个接口中方法重名问题 * */interface Machine { void run();} clas ...

  4. 2-17作业 数据库和shell综合练习

    1. 使用shell把“12306用户名和密码库-不要使用记事本打开会卡死-解压后可使用word或ultraedit打开.rar”中的所有记录成生sql语句,然后把sql导入数据库,成一个uPwd_1 ...

  5. jq的each理解

    1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...

  6. Python中的结构化数据分析利器-Pandas简介

    Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发tea ...

  7. Codeforces Round #394 (Div. 2)A水 B暴力 C暴力 D二分 E dfs

    A. Dasha and Stairs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. HRBUST 1819

    石子合并问题--圆形版 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 61(27 users) Total Accepted: 26( ...

  9. Oracle raw类型

    RAW(size):长度为size字节的原始二进制数据,size的最大值为2000字节; RAW类型好处:在网络中的计算机之间传输 RAW 数据时,或者使用 Oracle 实用程序将 RAW 数据从一 ...

  10. Mybatis(4) 映射文件-参数处理

    参数处理: 单参数处理: mybatis 不会做任何特殊处理. #{key} : key 可以写任何字段取出参数值. 测试方法: mapper接口: mapper.xml: 控制台: 多参数处理: m ...