hdu1024 Max Sum plus Plus

Source: Internet
Author: User

Dynamic programming, given an integer array of length n (≤1e6) and an integer m, selects m consecutive and 22 disjoint sub-ranges, which makes the interval and maximum maximum in all schemes.

DP[I][J] Indicates the end position (the position of the last element in the last interval) is I and the maximum value of the interval number j is selected.

It is easy to get the following state transition equations:

and

Taking into account the size of the array and the updated features of J, a one-dimensional scrolling array is used instead of a two-dimensional array, and the outermost loop enumerates J to M.

Use Dp[0][i] to denote dp[i][j], dp[1][i] for Max (Dp[k][j-1]) (k≤i).

The complexity is O (n^2).

1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 typedef __int64 LL;7 8 Const intINF =0x3f3f3f3f;9 Const intMAXN = 1e6 +Ten;Ten  One intS[MAXN]; ALL dp[2][MAXN]; - LL ans, maxi; - intN, M; the  - intMain () { -      while(~SCANF ("%d%d", &m, &N)) { -          for(inti =1; I <= N; i++) scanf ("%d", &s[i]); +Ans = Maxi =-inf; -Memset (DP,0,sizeofDP); +         into =1; A          for(intj =1; J <= M; J + +){ atMaxi =-inf; -              for(inti = j; I <= N; i++){ -dp[0][i] = S[i] + max (dp[0][i-1], dp[1][i-1]); -             } -              for(inti = j; I <= N; i++) maxi = dp[1][i] = max (maxi, dp[0][i]); -         } in          for(inti = m; I <= N; i++) ans = max (ans, dp[0][i]); -printf"%i64d\n", ans); to     } +     return 0; -}
View Code

hdu1024 Max Sum plus Plus

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.