POJ 2479 Maximum sum
来源:互联网 发布:龙族物语无法连接网络 编辑:程序博客网 时间:2024/04/30 15:47
Description
Your task is to calculate d(A).
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.
Output
Sample Input
1101 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13
Hint
Huge input,scanf is recommended.
思路:对于求数列中不相交的最大两个字段和,先从[0~i-1]的最大字段和从左向右扫描。[i~n-1]从右向左扫描即可
#include<cstdio>
#include<algorithm>
#define MIN -0xffffff //直接转十进制数值是-16777215
using namespace std;
int a[100010];
int dp[100010];
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int sum=0,count=MIN;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i]; //输入时控制找出从左向右扫描的最大值;
if(sum>count)
count=sum;
dp[i]=count;
if(sum<0)sum=0;
}
int S=MIN,m=0;
count=MIN;
for(int i=n-1;i>=1;i--)
{
m+=a[i];
if(m>count)count=m; //按照dp[n-2]是前n-1项的最大字段和,a[n-1]是最后一个数,
if(m<0)m=0; //dp[n-2]+a[n-1]相加与后面比较找出最大字段和,随后让dp[n-3]+count(a[n-2]与a[n-1]的最大字段和),后面同样,最终找出两个不相交最大字段和
if(count+dp[i-1]>S)S=count+dp[i-1];
}
printf("%d\n",S);
}
system("pause");
return 0;
}
- poj 2479 Maximum sum
- POJ 2479 Maximum sum
- POJ 2479 Maximum sum
- POJ 2479 Maximum sum
- poj 2479 Maximum sum
- Poj 2479 Maximum sum
- POJ-2479-Maximum sum
- POJ 2479 Maximum sum
- poj 2479 Maximum sum
- poj 2479 Maximum sum
- poj 2479 Maximum sum
- POJ 2479 Maximum sum
- POJ 2479 Maximum sum
- POJ 2479 Maximum sum
- poj 2479 - Maximum sum
- POJ 2479 Maximum sum
- POJ 2479 Maximum sum
- poj-2479 Maximum sum
- 我的随笔
- eof
- 数学之路-python计算实战(9)-机器视觉-图像插值仿射
- HDU 3980 Paint Chain(SG环链游戏)
- 【Thinking in java】运算符 & 流程控制
- POJ 2479 Maximum sum
- 面试算法(十三)调整数组顺序使奇数位于偶数前面
- 百度开源编辑器内容获取与更改迸
- ado.net(二)
- 测试专家blog
- 国内技术博客
- 1755: [Usaco2005 qua]Bank Interest
- Caused by: java.lang.ClassNotFoundException: org.hibernate.service.jta.platform.spi.JtaPlatform
- 1062. Talent and Virtue