The main problem: to find the largest and most of the non-intersecting intervals
Problem-solving ideas: This is a reference to others, portal
Organize yourself again, using dp[i][j] to indicate that the first J arrays comprise the largest of the I disjoint intervals, where the J number must be within a certain interval
Now, the decision is that the J-array is in a range of numbers and the other number, or is it a separate section of its own.
1. If the interval is in and other numbers, then dp[i][j] = dp[i][j-1] + num[j]
2. If the interval is independent, then dp[i][j] = Dp[i-1][k] + num[j]
Dp[i-1][k] = max (dp[i-1][i-2], dp[i-1][i-1] ... dp[i-1][j-1])
In summary, dp[i][j] = max (dp[i][j-1], dp[i-1][k]) + num[j]
Where Dp[i-1][k] can be recorded with a variable, record the maximum value of each dp[i-1][k]
Because each DP is only related to the previous one, it can be turned into a rolling array to solve the
#include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
const int N = 1000010;
const int INF = 0x7fffffff;
int n, m;
int dp[n][2], num[n], mmax[n];
void Solve () {for (int i = 1; I <= n; i++) {scanf ("%d", &num[i]);
Dp[i][0] = dp[i][1] = 0;
} Dp[0][0] = dp[0][1] = 0;
int Max;
for (int i = 1; I <= m; i++) {Max =-inf;
for (int j = i; J <= N; j + +) {dp[j][0] = max (dp[j-1][0] + num[j], dp[j-1][1] + num[j]);
DP[J-1][1] = Max;
max = max (max, dp[j][0]);
}} printf ("%d\n", Max);
} int main () {while (scanf ("%d%d", &m, &n)! = EOF) solve ();
return 0; }