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. CF978C Letters【前缀和+二分查找/几房几号】

    [链接]:CF978C [分析]:在前缀和数组种二分找到>=询问数的位置,根据位置就好操作了 [代码]: #include<bits/stdc++.h> using namespac ...

  2. bzoj3225 [Sdoi2008]立方体覆盖——扫描线

    3225: [Sdoi2008]立方体覆盖 Description A君近日为准备省队选拔,特意进行了数据结构的专项训练.训练过程中就遇到了“矩形面积并”这道经典问题,即:给出N个各边与坐标轴平行(垂 ...

  3. sql with multiply where

    I am wondering if this is a valid query: UPDATE table SET ID = 111111259 WHERE ID = 2555 AND SET ID ...

  4. Sort Transformed Array -- LeetCode

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  5. debug模式下dlgdata.cpp line 43 断言失败

    我在VC6下显示Line 43, Line 624行失败 网上有Line 40行猜测是其他版本 运行程序出错,定位如下: HWND CDataExchange::PrepareCtrl(int nID ...

  6. USE [EPPM] [dbo].[REFRDEL_CLEANUP]

    USE [EPPM] GO /****** Object: StoredProcedure [dbo].[REFRDEL_CLEANUP] Script Date: 2016/4/2 16:32:29 ...

  7. 在vs2010中编译log4cxx-0.10.0具体方法(从下载、编译、解决错误具体介绍)

    一. 简单介绍 log4cxx是Java社区著名的log4j的c++移植版.用于为C++程序提供日志功能,以便开发人员对目标程序进行调试和审计,log4cxx是apache软件基金会的开源项目,基于A ...

  8. mysql left join查询没走索引

    SELECT t0.ID as id, t0.`NAME` as name, t0.PHONE as phone, t0.`CITY_CODE` as cityCode, t0.SHOOTING_TI ...

  9. hdu 3667 /2010哈尔滨赛区H题 费用与流量为非线性关系/费用流

    题意: 在一般费用流题目改动:路过某路,每x单位流量须要花费 ai*x^2(ai为给定的系数). 開始的的时候,一看仅仅只是是最后统计费用上在改动罢了,一看例子.发现根本没那么简单(ps:以后每次写程 ...

  10. 2、cas4.0 单点登录 之 cas-client

    cas4.0 单点登录 之 cas-client cas4.0 单点登录 之 https证书已经做好了证书的准备工作.如今结合cas-server来配置单点登录: 一.安装cas服务端(cas-ser ...