Dividing each monotonous sequence into a group can be approached with the highest quality.
Then, based on this, we discuss the two endpoints of the monotone sequence of this section, which sequence they should be divided into, and record the optimal value.
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime > #include <iomanip> #pragma comment (linker, "/stack:1024000000"); #define EPS (1e-6) #define LL Long Long#define ULL unsigned long long#define _ll __int64#define INF 0x3f3f3f3f#define mod 1000000007#define mod 1000000007/** I/o Acceler Ator Interface. **/#define G (C=getchar ()) #define D isdigit (g) #define P x=x*10+c-' 0 ' #define N x=x*10+ ' 0 '-c#define pp l/=10,p#define nn l/= 10,ntemplate<class t> inline t& RD (T &x) {char C; while (!D); x=c-' 0 '; while (d) p; return x;} Template<class t> inline t& RDD (T &x) {char C; while (g,c!= '-' &&!isdigit (c)); if (c== '-') {x= ' 0 '-G; while (d) n; } else {x=c-' 0 '; while (d) p; } return x;} Inline double& RF (double &x)//sCANF ("%lf", &x); {char C; while (g,c!= '-' &&c!= '. &&!isdigit (c)); if (c== '-') if (g== '. ') {x=0; Double l=1; while (d) nn; X*=l; } else {x= ' 0 '-C; while (d) n; if (c== '. ') {Double l=1; while (d) nn; X*=l; }} else if (c== '. ') {x=0; Double l=1; while (d) pp; X*=l; } else {x=c-' 0 '; while (d) p; if (c== '. ') {Double l=1; while (d) pp; X*=l; }} return x;} #undef nn#undef pp#undef n#undef p#undef d#undef gusing namespace Std;int num[1000010];int Judge (int x,int y) {if (x = = Y) return 0; if (x < y) return-1; return 1;} _ll Dp[2][1000010];int Main () {int n,i,j; scanf ("%d", &n); for (i = 1;i <= n; ++i) scanf ("%d", &num[i]); int pre; MemseT (dp,0,sizeof (DP)); for (num[n+1] = Num[n],pre = 1,i = 2;i <= n; ++i) {if (Judge (Num[pre],num[i]) *judge (num[i],num[i+1]) = =-1 | | i = = N) {dp[0][i] = max (Dp[1][pre] + ABS (Num[i]-num[pre]), Dp[0][pre] + ABS (num[pre+1]-num[i)); Dp[1][i] = max (Dp[0][pre],dp[1][pre]); Dp[1][i] = max (Dp[1][i],dp[1][pre]+abs (num[pre]-num[i-1)); if (pre+1 <= i-1) dp[1][i] = max (Dp[1][i],dp[0][pre] + ABS (num[pre+1]-num[i-1])); Pre = i; }} printf ("%i64d\n", Max (Dp[0][n],dp[1][n])); return 0;}
484d-kindergarten DP