HDU 1024 Max sum plus DP

Source: Internet
Author: User

This question is to find the M-segment sub-segments, and require these sub-segments to be added up and the maximum, maximum, and plus versions.

However, the meaning of the question is really hard to understand. X and Y are not clear.

When I knew the question, I began to solve the problem. This must be a dynamic programming method.

The process of dynamic programming is not difficult to write. The key is abstract thinking.

The minimum condition here is that the maximum sub-segment and the problem are degraded when only one segment is divided. This is the minimum condition of the number of segments. If there is only 0, the result must be zero, or if there is only one number, it is the minimum condition of the array when there are only 0 or 1 columns.

Then, the record uses an array to record DP [max_n]. The DP [I] indicates the maximum value obtained when I is selected.

In this way, if the number of J is divided into I segments, DP [J] = max (DP [J-1] + arr [J], Max [J-1] + arr [J]), in fact, if you use a two-dimensional array, then Max [J-1] = DP [[I-1] [J-1] the value of this State Grid, indicating that when the I-1 segment is divided, there is a value of the number of J-1. Take Max is to indicate that arr [J] is directly and DP [J-1] is connected to the next I segment, or independent into a segment of the value is relatively large, choose the optimal solution, take the maximum value. The record data scheme here is called a rolling array. But I separate two arrays.

Abstract thinking is hard to understand, and the code is still very easy to write.

# Include <stdio. h> # include <vector> # include <string. h> # include <algorithm> # include <iostream> # include <string> # include <limits. h >#include <stack >#include <queue >#include <set> # include <map> using namespace STD; const int max_n = 1000001; int DP [max_n], max [max_n], arr [max_n]; int main () {int n, m, maxsum; while (~ Scanf ("% d", & M, & N) {memset (max, 0, sizeof (INT) * (n + 1); memset (DP, 0, sizeof (INT) * (n + 1); For (INT I = 1; I <= N; I ++) {scanf ("% d ", arr + I) ;}for (INT I = 1; I <= m; I ++) {maxsum = int_min; For (Int J = I; j <= N; j ++) {DP [J] = max (DP [J-1] + arr [J], Max [J-1] + arr [J]); Max [J-1] = maxsum; // not overflow heremaxsum = max (maxsum, DP [J]) ;}} printf ("% d \ n", maxsum) ;}return 0 ;}



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.