The length of the chopsticks from long to short arrangement, you can ignore the impact of Z (for each pair (x, y) can always be guaranteed to be greater than its z);
Then define the state Dp[i][j] to represent the minimum cost of the J pair in the front I chopsticks, and then:
If J<3*i is dp[i][j]=dp[i-1][j]
If the j>=3*i for the first chopsticks can be selected to form the J pair (and i-1 chopsticks constitute the J (x, Y)), of course, can also be selected;
Then Dp[i][j]=min (Dp[i-1][j],dp[i-2][j]+w (a[i],a[i-1]))
The code is as follows:
#include <iostream> #include <cstdio> #include <cstring>using namespace std; #define INF 0x3fffffffint A[10000];int dp[5500][1100];int n,k;void input () { scanf ("%d%d", &k,&n); for (int i=n;i>=1;i--) scanf ("%d", &a[i]);} void Solve () {for (int. i=0;i<=n;i++) for (int j=0;j<=n;j++) { dp[i][j]=inf; if (j==0) dp[i][0]=0; } for (int i=1;i<=n;i++) for (int j=1;j<=k+8;j++) { dp[i][j]=dp[i-1][j]; if (i>=j*3) { dp[i][j]=min (dp[i][j],dp[i-2][j-1]+ (a[i]-a[i-1]) * (a[i]-a[i-1])); } } printf ("%d\n", Dp[n][k+8]);} int main () { int t; scanf ("%d", &t); while (t--) { input (); Solve (); } return 0;} </span>
UVA 10271--chopsticks +DP