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 ;}