Poj 2479 Maximum sum
来源:互联网 发布:柠檬绿茶 淘宝 编辑:程序博客网 时间:2024/05/17 01:21
Maximum sum
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 39500 Accepted: 12340
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.
Output
Print exactly one line for each test case. The line should contain the integer d(A).
Sample Input
1
10
1 -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.
Source
POJ Contest,Author:Mathematica@ZSU
/*题意:给一段序列,将这个序列分成两部分,使两部分的最大子段和的和最大.o(n)维护最大前缀/后缀和.枚举断点将两端和加起来即可.正确性显然.*/#include<iostream>#include<cstdio>#define MAXN 50001using namespace std;int f[MAXN],n,s[MAXN],w[MAXN],w2[MAXN],ans;int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x*f;}void slove(){ f[1]=s[1];w[1]=s[1]; for(int i=2;i<=n;i++) { if(f[i-1]>=0) f[i]=f[i-1]+s[i]; else f[i]=s[i]; w[i]=max(w[i-1],f[i]); } f[n]=s[n];w2[n]=s[n]; for(int i=n-1;i>=1;i--) { if(f[i+1]>=0) f[i]=f[i+1]+s[i]; else f[i]=s[i]; w2[i]=max(w2[i+1],f[i]); } for(int i=1;i<=n-1;i++) ans=max(ans,w[i]+w2[i+1]); printf("%d\n",ans); return ;}int main(){ int t; t=read(); while(t--) { n=read();ans=-1e9; for(int i=1;i<=n;i++) s[i]=read(); slove(); } 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
- Cdll与windll
- Spark core 核心算子优化
- SpringBoot(二)开发WEB应用
- Linux-CentOS6.7——安装tftp服务器
- String类1
- Poj 2479 Maximum sum
- jsonp的原理
- 12-简单的系统救援模式
- 聚类分析经典算法讲解及实现
- java算法实现"输入一个英文句子,反转句子中的单词顺序,但单词内字符的顺序不变"
- 欢迎使用CSDN-markdown编辑器
- KNN--python--机器学习
- 【游戏设计模式】之三 状态模式、有限状态机 & Unity版本实现
- K邻近(KNN)分类和预测算法