Max Sum
2014-01-17 13:45
288 查看
Max Sum
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
There are N intergers make up a loop. Your job is to calculate the max sum of consecutive sub-suquence in the loop. Note, you can select a null consecutive sub-suquence.
For example, given (3, 1, -5, 2), the max sum is 2+3+1 = 6.
Input
The first line of the input contains an integer T (1<=T<=50) which means the number of est cases. Then T lines follow, and each line starts with a number N (1<=N<=100,000), then N integers followed (all the integers are between -1,000 and 1,000).
Output
For each test case, you should output one integers, the max sum.
Sample Input
3
4
3 1 -5 2
2
-4 -1
5
3 -1 4 -4 -2
Sample Output
6
0
6
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
There are N intergers make up a loop. Your job is to calculate the max sum of consecutive sub-suquence in the loop. Note, you can select a null consecutive sub-suquence.
For example, given (3, 1, -5, 2), the max sum is 2+3+1 = 6.
Input
The first line of the input contains an integer T (1<=T<=50) which means the number of est cases. Then T lines follow, and each line starts with a number N (1<=N<=100,000), then N integers followed (all the integers are between -1,000 and 1,000).
Output
For each test case, you should output one integers, the max sum.
Sample Input
3
4
3 1 -5 2
2
-4 -1
5
3 -1 4 -4 -2
Sample Output
6
0
6
题意:在一个环序列中,找出最大连续序列和 思路:求最大连续序列和 ,有两种情况:总和-最小连续序列和,或者是,最长连续序列和; 代码:
#include <cstdio> #include <iostream> #include <cstring> #include <vector> #include <algorithm> using namespace std; #define max(a ,b) a>b?a:b #define min(a ,b) a<b?a:b #define INF 1000005 #define PI atan(1.0)*4.0 #define N 1000005 int a ; int main() { freopen("in.txt" ,"r" ,stdin); freopen("out.txt" ,"w" ,stdout); int t; scanf("%d" ,&t); while(t--) { int n; scanf("%d" ,&n); int sum = 0; for(int i = 0 ; i < n ; i++) { scanf("%d" ,&a[i]); sum += a[i]; } int sump = 0 , max = 0 ,res = 0; for(int i = 0; i < n ; i++) { sump += a[i]; if(sump > 0) sump = 0; max = sump < max ? sump : max; } sum -= max; sump = 0; for(int i = 0 ; i < n ; i++) { sump += a[i]; if(sump < 0) sump = 0; res = sump > res ? sump : res; } res = res > sum ? res:sum; printf("%d\n" ,res); } return 0; }
相关文章推荐
- 动易2006序列号破解算法公布
- C#数据结构与算法揭秘二
- 浅析STL中的常用算法
- JavaScript 组件之旅(二)编码实现和算法
- Javascript SHA-1:Secure Hash Algorithm
- 将15位身份证补全为18位身份证的算法示例详解
- C++算法系列之日历生成的算法代码
- 简单的四则运算
- 数的奇偶性
- 1 2 3 4 5 6 7 8 9 = 110的java实现
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- 【代码】Pythonの代码片段
- STL中算法
- 数据结构&算法学习
- 算法的时间复杂度
- 算法导论:选择排序的原理与实现
- PHP实现四种常用的排序算法
- 图解插入排序算法
- 一些常见算法的JavaScript实现
- 平方根sqrt()函数的底层算法效率