POJ 2479 Maximum sum

http://poj.org/problem?id=2479

题意:

给出一个整数串,求连续子串1和连续子串2,不相交并且串1加串2的和最大。

思路:

其实就是求最大连续和,题意要求就是求两段最大连续和。我们可以从左边和右边分别求最大连续和,代码中的dp_l[i]就是1~i的最大连续和,dp_r[i]则是i~n的最大连续和

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn = 50000 + 5;
 8 const int INF = -10000000;
 9
10 int n;
11 int a[maxn];
12 int l[maxn], r[maxn];
13 int dp_l[maxn], dp_r[maxn];
14
15 int main()
16 {
17     //freopen("D:\\txt.txt", "r", stdin);
18     int T;
19     scanf("%d", &T);
20     while (T--)
21     {
22         scanf("%d", &n);
23         for (int i = 1; i <= n; i++)
24             scanf("%d", &a[i]);
25         l[0] = dp_l[0] = INF;
26         r[n+1] = dp_r[n+1]=  INF;
27         for (int i = 1; i <= n; i++)
28         {
29             l[i] = max(l[i - 1] + a[i], a[i]);
30             dp_l[i] = max(dp_l[i - 1], l[i]);
31         }
32         for (int i = n; i >= 1; i--)
33         {
34             r[i] = max(r[i + 1] + a[i], a[i]);
35             dp_r[i] = max(dp_r[i + 1], r[i]);
36         }
37         int ans = INF;
38         for (int i = 1; i <= n; i++)
39         {
40             ans = max(dp_l[i] + dp_r[i + 1], ans);
41         }
42         printf("%d\n", ans);
43     }
44     return 0;
45 }
时间: 2024-10-18 10:12:17

POJ 2479 Maximum sum的相关文章

[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

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]来保存  区间

poj 2479 Maximum sum(递推)

?? 题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n到i的最大连续子串和,这样将时间复杂度降为O(n). #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include&l

[poj 2479] Maximum sum -- 转载

转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                                      转载请注明出处:http://blog.csdn.net/wangjian8006 题目大意: 对于连续的整数和的串s1和s2,s1与s2不相交,使得s1+s2最大 解题方法: DP.  lt[i]代表以第i个元素结尾的串最大值  rt[i]代表以第i个元素开头的串的最大

POJ 2479 Maximum sum ( DP )

题目大意: 对整数串S,求其两个不相交的子串s1.s2,使得s1+s2的值最大. 方法:DP, lt[i]代表以第i个元素结尾的串最大值 rt[i]代表以第i个元素开头的串的最大值 那么设置一个rtm[i]代表取后i个元素之中最大连续子串的和 很显然,lt[i]=max(a[i],lt[i-1]+a[i]); rt[i]=max(a[i],rt[i+1]+a[i]); rtm[i]=max(rtm[i+1],rt[i]); #include <iostream> #include <cs

poj 2479 max sum

Maximum sumTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatus 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

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

poj-2479 Maximum sum 【最大字串和】

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