Maximum sum(最大子段和)

Maximum sum

AC_Code

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 #include <cstdlib>
 8 #include <algorithm>
 9 using namespace std;
10 typedef long long ll;
11 const int maxn = 50010;
12 const ll inf=0x3f3f3f3f;
13
14 int a[maxn];
15 int leftt[maxn],rightt[maxn], leftmax[maxn],rightmax[maxn];
16
17 int main()
18 {
19     int t;
20     scanf("%d",&t);
21     while( t-- ){
22         int n;
23         scanf("%d",&n);
24         for(int i=1;i<=n;i++){
25             scanf("%d",&a[i]);
26         }
27         leftt[1]=a[1];
28         leftmax[1]=a[1];
29         rightt[n]=a[n];
30         rightmax[n]=a[n];
31
32         for(int i=2;i<=n;i++){
33             leftt[i]=max(leftt[i-1]+a[i], a[i]);
34         }
35         for(int i=1;i<=n;i++){
36             leftmax[i]=max(leftmax[i-1],leftt[i]);
37         }
38         for(int i=n-1;i>=1;i--){
39             rightt[i]=max(rightt[i+1]+a[i],a[i]);
40         }
41         for(int i=n-1;i>=1;i--){
42             rightmax[i]=max(rightt[i],rightmax[i+1]);
43         }
44
45         int ans=-inf;
46         for(int i=2;i<=n;i++){
47             ans=max(ans,leftmax[i-1]+rightmax[i]);
48         }
49         printf("%d\n",ans);
50     }
51     return 0;
52 }

原文地址:https://www.cnblogs.com/wsy107316/p/12244808.html

时间: 2024-12-21 05:26:39

Maximum sum(最大子段和)的相关文章

Codeforces 75D Big Maximum Sum 最大子段和 dp

题目链接:点击打开链接 题意: 第一行 n m n个vector 下面n行 第一个数字u表示vector 的大小,然后后面u个数字给出这个vector 最后一行m个数字 表示把上面的vector拼接起来 得到一个大序列,求这个大序列的最大子段和 先预处理出每个vector的最大子段和,左起连续最大,右起连续最大,所有数的和 然后dp 一下.. #include <cstdio> #include <iostream> #include <algorithm> #incl

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

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

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

hdu 5586 Sum 最大子段和

Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5586 Description There is a number sequence A1,A2....An,you can select a interval [l,r] or not,all the numbers Ai(l≤i≤r) will become f(Ai).f(x)=(1890x+143)mod1

ural 1146. Maximum Sum

1146. Maximum Sum Time limit: 0.5 secondMemory 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 the sum of all the elements in that rectangle. In this p

POJ 2479 Maximum sum(双向DP)

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

Poj 2479 Maximum sum【双向DP/最大连续和】

Maximum sum 题意:给定一个长度为N的数组,求两个连续的子序列,使得两个连续子序列的和最大. 分析:乍一看,跟最大连续和有点类似,但是,又有区别,因为对于这个题,考虑第i项两个连续子序列的最大和,不能仅仅由前i-1项递推得出,第i项两个连续子序列的最大和,与前i项和i以后的之间是存在关系的,因此这个题目是一个双向dp. 假如给定的序列为a0, a1, a2, a3, a4, ...... ,an,那么,对于任意第i项,我以第i个元素为分界点,用一个数组项pMax [i]来保存  区间

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). 输入

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[