This is similar to the dual-tune DP in the previous question, but it is a bit different.
You can select a vertex as the initial vertex. First, right-click some of the vertices, right-click the vertices on the rightmost vertex, and then left-click the remaining vertices.
At the same time, the question requires that the last knock to the right and the first knock to the left are consecutive.
So similar to zoj 2581, but this time we started from the rightmost end, not two paths for two villains
Wa once, because it is infinitely small...
/*************************************** **************************************** # Author: neo Fung # Email: neosfung@gmail.com # Last modified: 2012-05-18 # filename: zoj2096 door to secret. CPP # description: **************************************** **************************************/# ifdef _ msc_ver # define debug # DEFINE _ crt_secure_no_deprecate # endif # include <fstream> # include <stdio. h> # Include <iostream> # include <string. h> # include <string> # include <limits. h> # include <algorithm> # include <math. h> # include <numeric> # include <functional> # include <ctype. h> # define Max 1010 using namespace STD; int num [Max], DP [Max] [Max]; int main (void) {# ifdef debug freopen (".. /stdin.txt "," r ", stdin); freopen (".. /stdout.txt "," W ", stdout); # endif int N, ncase = 1; // scanf (" % d ", & ncase); While (~ Scanf ("% d", & N) {for (INT I = n-1; I> = 0; -- I) scanf ("% d ", & num [I]); For (INT I = 0; I <n; ++ I) for (Int J = 0; j <n; ++ J) DP [I] [J] = 1 <16; DP [0] [0] = 0; For (INT I = 1; I <n; ++ I) for (Int J = 0; j <I; ++ J) {DP [I] [I-1] = min (DP [I] [I-1], DP [I-1] [J] + ABS (Num [J]-num [I]); // J goes to IDP [I] [J] = min (DP [I] [J], DP [I-1] [J] + ABS (Num [I-1]-num [I]); // I-1 goes to I} int ans = int_max; For (INT I = 0; I <n-1; ++ I) ans = min (DP [n-1] [I], ANS); printf ("% d \ n", ANS);} return 0 ;}