poj2479 && poj2593Maximum sum(求两个不相交最大字段的和)
来源:互联网 发布:淘宝美工多少钱一个月 编辑:程序博客网 时间:2024/05/17 07:57
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://poj.org/problem?id=2479
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
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.
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
1101 -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.
Huge input,scanf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
代码如下:
#include <cstdio>#include <iostream>using namespace std;#define INF 0x3fffffff#define M 100000+17int a[M],b[M];int main(){int n, i, T;while(~scanf("%d",&T)){while(T--){scanf("%d",&n);int sum = 0, MAX = -INF;for(i = 1; i <= n; i++){scanf("%d",&a[i]);sum+=a[i];if(sum > MAX){MAX = sum;}b[i] = MAX;if(sum < 0){sum = 0;}}MAX = -INF;sum = 0;int ans = MAX, t;for(i = n; i > 1; i--){sum+=a[i];if(sum > MAX){MAX = sum;}t = MAX + b[i-1];if(t > ans){ans = t;}if(sum < 0){sum = 0;}}printf("%d\n",ans);}}return 0;}
5 0
- poj2479 && poj2593Maximum sum(求两个不相交最大字段的和)
- poj2479 Maximum sum 求最大的 数列中不相交的俩个子段和
- POJ 2479 Maximum sum(dp—求最大的两个不相交的字段和)
- 【动态规划】poj2479 Maximum sum && poj2593 Max Sequence(求两段不相交字串的最大和)
- poj2593 Max Sequence(求两个不相交最大字段和)
- POJ2479,动态规划求最大字段和
- POJ 2479 Maximum sum (求2个不相交的连续字段和的最大值)
- poj2479-在一个数组中求任意不相交的两个子数组之和的最大值
- 最大连续两段不相交字段和(poj2594,poj2479)
- 【原题】求两个不相交的连续子数组的最大和
- 找出两个不相交连续子数组的最大和
- poj2479 Maximum sum(最大子段和变形)
- 【动态规划(最大子序列和)】:poj2479,Maximum sum
- HDU 1024 Max Sum Plus Plus(求m个不相交连续子序列最大和/01背包)
- 1486【动态规划★★】Max Sum Plus Plus.(求n个数m个不相交子段的最大和)
- hdu 5406 CRB and Apple 求两个不相交的不下降子序列之和最大
- poj2479最大数的和
- HDU1003 Max Sum(求最大字段和)
- 链表的基本操作
- Nagios监控MySQL报错:NRPE: Unable to read output的详细解决过程
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- Linux下永久修改MAC地址和ifconfig命令总结
- 马踏棋盘
- poj2479 && poj2593Maximum sum(求两个不相交最大字段的和)
- 子进程继承父进程中的锁
- POJ-3187 Backward Digit Sums
- cocos2d html5 cc.action调用函数
- 初学C语言 C语言的认识
- C++链接顺序问题
- as3.0遮罩导致的MouseEvent被遮盖的问题
- keep looking, do not settle
- bzoj1754 [Usaco2005 qua]Bull Math