【HDOJ】1421 搬寝室

时间:2024-01-21 23:46:45

DP。这题都能TLE,发现memset有时居然比for还要慢。

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define MAXN 2005
#define INF 0x3fffffff
int dp[MAXN][MAXN];
int buf[MAXN];
int deg[MAXN]; int mymin(int a, int b) {
return (a<b) ? a:b;
} int comp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
} int main() {
int n, m, k;
int i, j; while (scanf("%d %d", &n, &k) != EOF) {
for (i=; i<=n; ++i)
scanf("%d", &buf[i]);
qsort(buf+, n, sizeof(int), comp);
m = ;
for (i=; i<=n; ++i)
deg[m++] = (buf[i]-buf[i-])*(buf[i]-buf[i-]); for (i=; i<=n; ++i) {
dp[i][] = ;
for (j=; j<=k; ++j) {
dp[i][j] = INF;
}
} for (i=; i<=n; ++i) {
for (j=; j+j<=i; ++j) {
dp[i][j] = mymin(dp[i-][j-]+deg[i-], dp[i-][j]);
}
}
printf("%d\n", dp[n][k]);
} return ;
}