poj2479
来源:互联网 发布:程序员团队名称和口号 编辑:程序博客网 时间:2024/06/14 13:57
Maximum sum
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 25093 Accepted: 7623
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.
这是一个求最大子段和的问题。但是是两个最大子段和的问题。首先从左到右求最大子段和,再从右到左求最大子段和将其相加。。
#include<iostream>#include<stdio.h>using namespace std;int a[50000+1];int left1[50000+1];int right1[50000+1];int main(){ freopen("in.txt","r",stdin); int t; int n; scanf("%d",&t); while(t) { t--; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } left1[1]=a[1];//从左到右求最大子段和。。 for(int i=2;i<=n;i++)//求前i个元素的最大子段和 { left1[i]=(left1[i-1]>0)?(left1[i-1]+a[i]):a[i]; } for(int i=2;i<=n;i++) { left1[i]=max(left1[i-1],left1[i]); } right1[n]=a[n];//从右到左求最大子段和 for(int i=n-1;i>=1;i--) { right1[i]=(right1[i+1]>0)?(right1[i+1]+a[i]):a[i]; } for(int i=n-1;i>=1;i--)//求从i到n的最大子段和 { right1[i]=max(right1[i+1],right1[i]); } int max=left1[1]+right1[2]; for(int i=2;i<n;i++) { if(max<left1[i]+right1[i+1]) { max=left1[i]+right1[i+1]; } } cout<<max<<endl; } return 0;}
- poj2479
- poj2479
- poj2479
- POJ2479
- POJ2479
- poj2479
- poj2479
- poj2479
- POJ2479&POJ2593
- poj2479(DP)
- POJ2479 DP
- POJ2479 Maximum sum
- POJ2479 Maximum sum
- poj2479 - Maximum sum
- POJ2479 第一次的DP....
- POJ2479 Maximum sum DP
- poj2479 Maximum sum
- POJ2479 Java 动态规划
- HDU 1505
- Artoolkit Android
- 用python实现一个栈
- 用js实现跑马灯效果
- 深入理解Linux内核之内存寻址笔记-2
- poj2479
- ARM汇编指令集
- Android中的模块(module)与设备(device)
- 用例图中关系的理解
- UISegmentedControl分段按钮的使用
- Linux笔记-文件系统
- BOJ 292 MABODX
- ListView 的使用
- jquery常用的插件1000收集