标签:dp
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#define N 5005
#define M 1005
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f;
int num[N], dp[N][M];
int main() {
int T;
scanf("%d", &T);
while (T--) {
int k, n;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
dp[i][j] = INF;
}
}
scanf("%d %d", &k, &n);
k += 8;
for (int i = 1; i <= n; i++) {
scanf("%d", &num[i]);
dp[i][0] = 0;
}
dp[0][0] = 0;
for (int i = n - 2; i >= 1; i--) {
for (int j = k; j >= 1; j--) {
dp[i][j] = dp[i + 1][j];
if (dp[i + 2][j - 1] != INF && n - i - j * 3 >= -1) {
dp[i][j] = min(dp[i][j], dp[i + 2][j - 1] + (num[i] - num[i + 1]) * (num[i] - num[i + 1]));
}
}
}
printf("%d\n", dp[1][k]);
}
return 0;
}
标签:dp
原文地址:http://blog.csdn.net/llx523113241/article/details/44944863