Max Sub-sequence and Hdoj 1003 Max Sum

Source: Internet
Author: User
Tags bitset cmath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.