转载请注明出处:http://blog.csdn.net/u012860063?

viewmode=contents

题目链接:

id=2593">http://poj.org/problem?id=2593

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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
 思想:对于数据a[],从左向右依次求解以a[i]结尾的最大子段和b[i],
  然后,从右向左遍历,求a[i]右边(包含a[i])的最大子段和sum,输出sum+b[i-1]的  最大值。

代码例如以下:

#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define M 100000+17
int a[M],b[M];
int main()
{
int n,i;
while(cin >> n && n)
{
int sum = 0, MAX = -INF;
for(i = 1; i <= n; i++)
{
cin >> a[i];
sum+=a[i];
if(sum > MAX)
{
MAX = sum;
}
b[i] = MAX;
if(sum < 0)
{
sum = 0;
}
}
MAX = -INF;
sum = 0;
int ans = MAX, t;
for(i = n; i > 1; i--)
{
sum+=a[i];
if(sum > MAX)
{
MAX = sum;
}
t = MAX + b[i-1];
if(t > ans)
{
ans = t;
}
if(sum < 0)
{
sum = 0;
}
}
cout<<ans<<endl;
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

poj2593 Max Sequence(两个不相交字段的最大总和与)的更多相关文章

  1. Codeforces Round #384 (Div. 2) A B C D dfs序+求两个不相交区间 最大权值和

    A. Vladik and flights time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. (线性dp,最大连续和)Max Sequence

    Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18511   Accepted: 7743 Des ...

  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 2593&&2479:Max Sequence

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

  5. 带有两个输入字段和相关标记的简单 HTML 表单:

    带有两个输入字段和相关标记的简单 HTML 表单: 意思就是说Male 和id="male"绑定在一起. <html> <body>   <p> ...

  6. POJ 2593 Max Sequence

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

  7. MySQL两个日期字段相减得到秒的方法

    一.MySQL中两个DateTime字段相减 假定表名为tblName,两个DateTime字段名分别为beginDateTime,endDateTime,以下是相关两个mysql日期字段相减的SQL ...

  8. SQL SERVER数据库,按年、月、日、时、分、秒计算两个时间字段之间的间隔时间样例

    使用DATEDIFF(取值,时间字段1,时间字段2) 举例: SELECT DATEDIFF(YEAR,DRYSJ,DCYSJ),* FROM YXHIS2019..TBZYBR2019 --SQL ...

  9. elementui中的el-table中拼接两个列表字段

    我们知道,在ElementUI中我们是使用下面的语法来展示列表字段的: <el-table :data="yanggbs" stripe style="width: ...

随机推荐

  1. 解决SMARTFORMS 中table 控件单行跨页的问题

    在CX项目中,MM模块做了大量的的单据打印的工作,一个问题困扰了我好久,一直不能解决.当物料描述很长时,table控件在单元格中能自动换行,这样就有可能在换页处出现一行记录的一部分打在上一页,一部分记 ...

  2. uva 10671 - Grid Speed(dp)

    题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...

  3. 国内三大PTPrivate Tracker站分析

    除这一行外,下面全部内容都是转载.出处不明. 国内三大PT(Private Tracker)站分析 先郑重的声明一下:本文以下的内容所有是复制粘贴的,不代表老夫的观点. 事实上内容我也没细致看. 贴这 ...

  4. hdu 3333 树状数组+离线处理

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了非常久不知道怎么处理,并且答案没看懂,然后找个样例模拟下别人的代码立即懂了---以后看不懂的 ...

  5. Andorid Async-HttpClient阅览

    Async-httpclient它是一个用于Android应用程序开发http访问开源框架.开源GitHub在,因为今天GitHub下载没有看到.我在这里提供下载地址.是之前从GitHub上下载的,版 ...

  6. 【从翻译mos文章】不再用par file如果是,export or import 包含大写和小写表名称表

    不再用par file如果是,export or import 包含大写和小写表名称表 参考原始: How to Export or Import Case Sensitive Tables With ...

  7. HDU 1054 Strategic Game(树形DP)

    Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...

  8. UVa 11621 - Small Factors

    称号:发现没有比给定数量少n的.只要2,3一个因素的数字组成. 分析:数论.贪婪,分而治之. 用两个三分球,分别代表乘法2,和繁殖3队列,队列产生的数字,原来{1}. 然后.每取两个指针相应元素*2和 ...

  9. system strategies of Resources Deadlock

    In computer science, Deadlock is a naughty boy aroused by compete for resources. Even now,     there ...

  10. 有关Struts2a的ction直接使用response异步问题

    假设我们在项目中使用struts2,正在使用ajax而通信时后端程序.为简单起见,我们经常使用下面的方法:         ActionContext ac = ActionContext.getCo ...