Obviously, after the items are sorted by weight, if you want to move a pair of items, these two items must be adjacent.
Then the order of the first item to consider the article I put or not put, that is to get the state transfer equation:
F[i][j] = min (f (i-1, J), F (i-2, j-1) + d[i]);
Among them, D[i] indicates the fatigue caused by the pair of articles I and article i-1.
1#include <algorithm>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intN =2001;7 Const intM =1001;8 intF[n][m];9 intC[n];Ten intN, K; One A intFintIintj) - { - if(J *2> i)return 999999999; the if(i = =0|| i = =1)return 0; - returnF[i][j]; - } - + intMain () - { + while(SCANF ("%d%d", &n, &k)! =EOF) A { at for(inti =1; I <= N; i++ ) - { -scanf"%d", C +i); - } -Sort (c +1, C +1+n); - for(inti =2; I <= N; i++ ) in { - for(intj = I/2; J >0; j-- ) to { + intD = (C[i]-c[i-1]) * (C[i]-c[i-1] ); -F[i][j] = min (F (i-1, j), F (i-2, J-1) +d); the } * } $printf"%d\n", F[n][k]);Panax Notoginseng } - return 0; the}
HDU 1421 Classic DP