題意 中文
先把物品重量從小到大排序 d[i][j]表示前i件物品選j對的最小疲勞那麼就有轉移方程d[i][j]=min(d[i-1][j],d[i-2][j-1]+(w[i]-w[i-1])^2)
d是初始化為無窮大的 所以i=j*2時 d[i-1][j]是等於無窮大的 所以狀態轉移方程可以統一為
d[i][j]=min(d[i-1][j],d[i-2][j-1]+(w[i]-w[i-1])^2)
#include#include #include using namespace std; const int N = 2014; int w[N], d[N][N / 2], n, k; int main() { while (~scanf ("%d%d", &n, &k)) { for (int i = 1; i <= n; ++i) scanf ("%d", &w[i]); sort (w + 1, w + n + 1); memset (d, 0x7f, sizeof (d)); for (int i = 0; i <= n; ++i) d[i][0] = 0; for (int i = 2; i <= n; ++i) for (int j = 1; j * 2 <= i; ++j) d[i][j] = min (d[i - 1][j], d[i - 2][j - 1] + (w[i] - w[i - 1]) * (w[i] - w[i - 1])); printf ("%d\n", d[n][k]); } return 0; }
2 1 1 3
4