Question: Give you an array, let you divide the continuous number into a group, the value of each group is the range of the number of this group, ask you how to group this to make the array range and maximum
Solution:
It can be divided into four situations for discussion.
Dp [I] indicates the maximum value that can be obtained from the first I + 1 item.
State transition equation
A [i-1] <a [I] <a [I + 1] dp [I] = dp [i-1] + a [I + 1]-a [I]
A [i-1]> a [I] <a [I + 1] dp [I] = max (dp [i-1], dp [i-2] + a [I + 1]-a [I])
A [i-1]> a [I]> a [I + 1] dp [I] = dp [i-1]-a [I + 1] + a [I]
A [i-1] <a [I]> a [I + 1] dp [I] = max (dp [i-1], dp [i-2]-a [I + 1] + a [I])
Dp [n-1] is what we want
Solution code:
1 # include <cstdio> 2 # include <iostream> 3 # include <algorithm> 4 5 using namespace std; 6 7 typedef long ll; 8 9 const int maxn = 1000010; 10 11 ll dp [maxn]; 12 int a [maxn]; 13 14 int main () 15 {16 int n; scanf ("% d", & n ); 17 for (int I = 1; I <= n; I ++) scanf ("% d", & a [I]); 18 dp [0] = 0; 19 for (int I = 1; I <n; I ++) 20 if (a [I + 1]> = a [I]) 21 {22 if (I = 1) | (a [I]> = a [i-1]) dp [I] = dp [i-1] + a [I + 1]-a [I]; else dp [I] = max (dp [i-1], dp [i-2] + a [I + 1]-a [I]); 23} 24 else25 {26 if (I = 1) | (a [I] <a [i-1]) dp [I] = dp [i-1]-a [I + 1] + a [I]; else dp [I] = max (dp [i-1], dp [i-2]-a [I + 1] + a [I]); 27} 28 cout <dp [n-1] <endl; 29 return 0; 30}
View Code
From SanSiroWaltz
Codeforces 484 (#276 Div 1) D Kindergarten DP