1481:Maximum sum

1481:Maximum sum

总时间限制: 
1000ms

内存限制: 
65536kB
描述
Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:

                     t1     t2          d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n }                    i=s1   j=s2

Your task is to calculate d(A).

输入
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.
输出
Print exactly one line for each test case. The line should contain the integer d(A).
样例输入
1

10
1 -1 2 2 3 -3 4 -4 5 -5
样例输出
13

//最大连续和的变种,dp数组表示以i结尾的子串最大和,l数组表示i往左最大的一个和,后二者表示反过来
#include<iostream>#include<memory.h>using namespace std;int dp[50001], rdp[50001], l[50001], r[50001], data[50001];int main() {    int T; scanf("%d", &T);    while (T --) {        int n;        scanf("%d", &n);        for (int i = 0; i < n; i ++) scanf("%d", &data[i]);        dp[0] = data[0]; rdp[n - 1] = data[n - 1];        memset(l, 0, sizeof(l));        memset(r, 0, sizeof(r));        l[0] = dp[0]; r[n - 1] = rdp[n - 1];        for (int i = 1; i < n; i ++) {            dp[i] = data[i];            if (dp[i - 1] > 0) dp[i] += dp[i - 1];            l[i] = max(l[i - 1], dp[i]);        }        for (int i = n - 2; i >= 0; i --) {            rdp[i] = data[i];            if (rdp[i + 1] > 0) rdp[i] += rdp[i + 1];            r[i] = max(r[i + 1], rdp[i]);        }        int ans = -2147483646;//注意全为负数的情况        for (int i = 0; i < n - 1; i ++)            ans = max(ans, l[i] + r[i + 1]);        printf("%d\n", ans);    }}

时间: 2024-08-11 05:46:23

1481:Maximum sum的相关文章

maximum sum

uva,10684 1 #include <iostream> 2 #include <cstdio> 3 #define maxn 10005 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 int a[maxn]; 10 while(scanf("%d",&n),n) 11 { 12 for(int i=0;i<n;i++) 13 scanf("%d",&a[

sicily 1091 Maximum Sum (动规)

1 //1091.Maximum Sum 2 //b(i,j) = max{b(i,j-1)+a[j], max(b(i-1,t)+a[j])} (t<j) 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 int t; 8 cin>>t; 9 while (t--) { 10 int n; 11 cin>>n; 12 int a[n+1]; 13 for (int i = 1; i &

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

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

UVA108 - Maximum Sum(最大连续和)

题意:从一个n*n的矩阵中找出和最大的子矩阵 思路:最大连续和的求解.我们可以将二维的转化为一维进行计算.sum[i][j]表示以(1, 1)和(i, j)为对角的矩阵的和.之后只要逐个枚举每个可能出现的值,保存最大值即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3

Round #169 (Div. 2)C. Little Girl and Maximum Sum

1.用退化的线段树(也就是没有区间查询)做... 2.注意longlong. #include<cstdio>#include<cstring> #include<iostream>#include<algorithm>using namespace std;int n,q;int a[200010],s[200010];int main(){    scanf("%d%d",&n,&q);for(int i=1;i<

Codeforces Round #169 (Div. 2)C. Little Girl and Maximum Sum

传送门 Description The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries,

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

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 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 o

URAL 1146. Maximum Sum(求最大子矩阵和)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1146 1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is

【UVA】108 - Maximum Sum(DP矩阵)

n^3的复杂度计算最小子矩阵,用了最大连续和的DP算法. 14273282 108 Maximum Sum Accepted C++ 0.013 2014-09-27  #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF = 1 << 30; const int maxn = 127 + 3; int mat[maxn][maxn]