A - Maximum sum

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit 

cid=84333#status//A/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); color:rgb(85,85,85)">Status 

id=19810" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); color:rgb(85,85,85)">Practice 

id=19810" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); color:rgb(85,85,85)">POJ
2479

Description

Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:

Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input. 

Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer. 



Huge input,scanf is recommended.
/*
Author: 2486
Memory: 544 KB Time: 438 MS
Language: C++ Result: Accepted
*/
//题目思路是从左到右分别求出它们所在位置的最大连续和,然后从右到左求出它们所在的最大连续和
//接着就是a[i]+b[i+1],a数组代表着从左到右,b代表着从右到左所以不断的比較a[0]+b[1],a[1]+b[2]求最大值就可以
//如何求解最大值(代码最后段有具体解释)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=50000+5;
int n,m;
int a[maxn];
int dp[maxn];
int main() {
scanf("%d",&n);
while(n--) {
scanf("%d",&m);
for(int i=0; i<m; i++) {
scanf("%d",&a[i]);
}
dp[0]=a[0];
int sum=a[0];
int ans=a[0];
for(int i=1; i<m; i++) {
if(sum<0) {
sum=0;
}
sum+=a[i];
if(sum>ans) {
ans=sum;
}
dp[i]=ans;
}
sum=a[m-1];
int Max=dp[m-2]+sum;
ans=a[m-1];
for(int j=m-2; j>=1; j--) {
if(sum<0) {
sum=0;
}
sum+=a[j];
if(sum>ans) {
ans=sum;
}
Max=max(Max,dp[j-1]+ans);
}
printf("%d\n",Max);
} return 0; }

假设当前的数据和小于零。非常明显,将它增加到后面的计算中,肯定会降低最大值

非常easy的道理。-1+4<0+4,假设之前的取值小于零,抛弃它,又一次赋值为零

然后通过maxs不断更新当前的最大值

while(true){

            scanf("%d",&a);

            if(sum<0) {

                sum=0;

            }

            sum+=a;

            if(sum>maxs) {

                maxs=sum;

            }

}

Maximum sum-动态规划的更多相关文章

  1. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

  2. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

  3. [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  4. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  5. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  6. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  7. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  8. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  9. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  10. Find the Maximum sum

    Given an array of n elements.Find the maximum sum when the array elements will be arranged in such w ...

随机推荐

  1. 51nod 1087 1 10 100 1000【打表】

    题目来源: Ural 1209 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 1,10,100,1000...组成序列1101001000...,求 ...

  2. 洛谷——P3395 路障

    P3395 路障 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1,1)这个点,他要走到(n,n)这个点. B君每秒可以向上下左右的某个方 ...

  3. bzoj 5346: tree (其实是是某次雅礼集训的题)

    用prufer序列的公式直接dp,O(n^4)的算法简简单单就写出来了23333. 按理说 O(n^4)是需要优化成O(n^3)才能过的,然鹅我也不知道我怎么过了23333 (那就懒得优化了hhhhh ...

  4. SQL表操作

    //创建一个名为TEST1的表空间 CREATE SMALLFILE TABLESPACE "TEST1" DATAFILE 'G:\ORACLE_11G\ORADATA\ORCL ...

  5. 集合框架(06)Arrays

    Arrays Arrays:用于操作数组的工具类,里面都是静态方法 ---数组变集合 1.asList:将数组变成List集合 把数组变成list集合的好处?可以使用集合的思想和方法来操作数组中的元素 ...

  6. sqlite3特殊的字符转义

    sqlite3数据库在搜索的时候,一些特殊的字符需要进行转义, 具体的转义如下:      /   ->    //     '   ->    ''     [   ->    / ...

  7. 【转】matlab 字符串处理函数

    原文地址 matlab 字符串处理函数 % 字符串处理 a='  a';b='b  b';c='cccc';m='' % 获取字符串长度 length(a)     % 连接两个字符串,每个字符串最右 ...

  8. 关于select 控件

    通过http://www.w3school.com.cn/tiy/t.asp?f=html_select 的测试,测得,select 控件值最多106个. Q:easyui的datagrid中能做到 ...

  9. virtualenv 和apache 搭建django时注意事项

    在对应网站的xxx.conf中 WSGIDaemonProcess http://127.0.0.1:80 python-path=/home/user/my_blog_2:/home/user/ve ...

  10. 【Todo】【转载】Java中的锁机制2 - Lock

    参考这篇文章 http://blog.csdn.net/chen77716/article/details/6641477 上一篇 (http://www.cnblogs.com/charlesblc ...