Topic Portal
1 /*2 Test instructions: max continuous subsequence and two endpoints3 Cumulative Traversal algorithm O (n): Blocks the sequence according to Sum<0, and the maximum value is generated on a piece of the block. DP is the same idea: dp[i] = max (Dp[i-1] + a[i], a[i]) is actually the same4 1003 is that so hard?? 5 Detailed explanation6 */7 /************************************************8 * Author:running_time9 * Created time:2015-8-10 17:44:42Ten * File name:o (n). cpp One ************************************************/ A -#include <cstdio> -#include <algorithm> the#include <iostream> -#include <sstream> -#include <cstring> -#include <cmath> +#include <string> -#include <vector> +#include <queue> A#include <deque> at#include <stack> -#include <list> -#include <map> -#include <Set> -#include <bitset> -#include <cstdlib> in#include <ctime> - using namespacestd; to + #defineLson L, Mid, RT << 1 - #defineRson mid + 1, R, RT << 1 | 1 thetypedefLong Longll; * Const intMAXN = 1e6 +Ten; $ Const intINF =0x3f3f3f3f;Panax Notoginseng Const intMOD = 1e9 +7; - intA[MAXN]; the + intMainvoid) {//hdoj 1003 Max Sum A intT, CAS =0; scanf ("%d", &T); the while(t--) { + intN scanf ("%d", &n); - for(intI=1; i<=n; ++i) { $scanf ("%d", &a[i]); $ } - intsum = a[1], mx = a[1]; - intL =1, r =1; the intLL =1, rr =1; - for(intI=2; i<=n; ++i) {Wuyi if(Sum <=0) { thesum = A[i]; L =i; - } Wu ElseSum + =A[i]; - if(MX <sum) { Aboutmx = sum; ll = l, rr =i; $ } - } - -printf ("Case %d:\n%d%d%d\n", ++CAs, MX, LL, RR); A if(T) puts (""); + } the - return 0; $}
1 /************************************************2 * Author:running_time3 * Created time:2015-8-10 18:24:004 * File Name:On_2.cpp5 ************************************************/6 7#include <cstdio>8#include <algorithm>9#include <iostream>Ten#include <sstream> One#include <cstring> A#include <cmath> -#include <string> -#include <vector> the#include <queue> -#include <deque> -#include <stack> -#include <list> +#include <map> -#include <Set> +#include <bitset> A#include <cstdlib> at#include <ctime> - using namespacestd; - - #defineLson L, Mid, RT << 1 - #defineRson mid + 1, R, RT << 1 | 1 -typedefLong Longll; in Const intMAXN = 1e6 +Ten; - Const intINF =0x3f3f3f3f; to + intA[MAXN]; - the intMainvoid) { * intT, CAS =0; scanf ("%d", &T); $ while(t--) {Panax Notoginseng intN scanf ("%d", &n); - for(intI=1; i<=n; ++i) { thescanf ("%d", &a[i]); + } A intsum =0, mx =-inf, MN =0; the intL =0, LL =0, rr =1; + for(intI=1; i<=n; ++i) { -Sum + =A[i]; $ if(Sum-mn >mx) { $mx = sum-mn; -ll = l; RR =i; - } the if(Mn >sum) { -MN = sum; L =i;Wuyi } the } -printf ("Case %d:\n%d%d%d\n", ++cas, MX, ll+1, RR); Wu if(T) puts (""); - } About $ return 0; -}
an alternative approach
Max Sub-sequence and Hdoj 1003 Max Sum