链接:http://pat.zju.edu.cn/contests/pat-a-practise/1007
题意:经典的最大连续子串问题。
分析:采用最优的O(N) 算法。
#include<stdio.h> int seq[10005]; int main() { // freopen("in.txt", "r", stdin); int n; scanf("%d", &n); int i; for (i = 0; i < n; i++) scanf("%d", &seq[i]); int maxSum = -1; int left, right; int thisSum = 0; int start = 0; int allNeg = 1; for (i = 0; i < n; i++) { if (seq[i] >= 0) allNeg = 0; thisSum += seq[i]; if (thisSum < 0) { thisSum = 0; start = i + 1; } else if (thisSum > maxSum) { maxSum = thisSum; left = seq[start]; right = seq[i]; } } if (allNeg) printf("%d %d %d\n", 0, seq[0], seq[n - 1]); else printf("%d %d %d\n", maxSum, left, right); return 0; }
相关推荐
Finding Maximum Contiguous Subsequence Sum using divide-and-conquer approach
1. Four Elements Sum 2. Rotten Oranges 3. Digit Count [How many X's] 4. Save Ironman 5. Closest Palindrome 6. Check Array Contains Contigous Integer 7. Pairs with Positive and Negative Values 8. ...
LCS Longest (maximum) common subsequence
2.4 Maximum subarray sum . . . . . . . . . . . . . . . . . . . . . . . . . 21 3 Sorting 25 3.1 Sorting theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.2 Sorting in C++ . . ...
前言k-Maximum Subsequence Sum - 模型结合这个模型的性质,我们可以发现我们每次要么选择一个与已有区间完全不相交的区间,要么从已有区间中
The Maximum Subsequence is the continuous subsequence which has the largest sum of...Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
动态规划 poj Common Subsequence c++ cpp文件
英文第二版 目录 . . . . . . . ....2.8 War Story: Mystery of the Pyramids ....2.9 Advanced Analysis (*) ....2.10 Exercises ....3.1 Contiguous vs....3.2 Stacks and Queues ....3.3 Dictionaries ....3.4 Binary Search Trees ....
最大子序列和算法的运行时间 衡量执行时间
Longest Ordered Subsequence,算法分析与设计,C语言程序
北大POJ2533-Longest Ordered Subsequence【O(nlogn)】
最大子序列和该算法采用向量(数组)并找到它的最大子序列和: node max-subsequence-sum.js [2,-4,6,8,-10,100,-6,5]
MaxSumTest.cpp: Various maximum subsequence sum algorithms Fig02_09.cpp: Test program for binary search Fig02_10.cpp: Euclid's algorithm, with a test program Fig02_11.cpp: Recursive exponentiation ...
Extracting Article Text from the Web with Maximum Subsequence Segmentation 论文 MMS算法。
Pku acm 第2533题 Longest Ordered Subsequence 代码,有详细的注释,动态规划
Column 8: Compute the maximum-sum subsequence in an array maxsum.c -- Time four algs: n3, n2, n log n, n. Column 9: Code tuning programs genbins.c -- Profile this, then try a special-purpose ...
最长公共子序列问题,动态规划法
动态规划:最长单调递增子序列 A numeric sequence of ai is ordered if a1 < a2 < ......Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given ...Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Pku acm 第1458题 Common Subsequence 代码,有详细的注释,动态规划