TYVJ 1125 JR's chop 解题报告
这题比较悲剧吧,始终只有80分,到最后才发现数组开销了一个1,应该是[101][51],我开的[101][50],然后。。。。80分咯~
代码如下:
#include <stdio.h> #include <stdlib.h> #define min(a, b) (((a)<(b))?(a):(b)) int num[101]; int f[101][51]; int com(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main(int argc, char **argv) { int i, j; int n, k; scanf("%d%d", &n, &k); k += 3; if(n < 2 * k){ printf("-1\n"); return 0; } for(i = 1; i <= n; i++){ scanf("%d", &num[i]); } qsort(&num[1], n, sizeof(int), com); for(i = 0; i <= n; i++){ f[i][0] = 0; for(j = 1; j <= k; j++){ f[i][j] = 0xFFFFFFF; } } for(i = 2; i <= n; i++){ for(j = 1; j <= min(i / 2, k); j++){ if((f[i - 2][j - 1] + (num[i] - num[i - 1]) * (num[i] - num[i - 1])) < f[i - 1][j]){ f[i][j] = f[i - 2][j - 1] + (num[i] - num[i - 1]) * (num[i] - num[i - 1]); }else{ f[i][j] = f[i - 1][j]; } } } printf("%d\n", f[n][k]); return 0; }