NOIP 2005 过河 解题报告
这题考的是压缩,当两个石子之间的距离大于100的时候就让他们变成100,然后计算就十分方便了,代码如下:
#include <stdio.h> #include <stdlib.h> #define min(a, b) ((a)<(b)?(a):(b)) int num[101]; int f[10101]; int stone[10101]; int com(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main(int argc, char **argv) { int i, j, k; int l, s, t, m; scanf("%d%d%d%d", &l, &s, &t, &m); for(i = 1; i <= m; i++){ scanf("%d", &num[i]); } if(s == t){ for(i = 1, j = 0; i <= m; i++){ if(num[i] % s == 0){ j++; } } printf("%d\n", j); return 0; } qsort(num, m + 1, sizeof(int), com); for(i = 1, j = 0; i <= m; i++){ if(num[i] - num[i - 1] > 100){ j += 100; stone[j] = 1; }else{ j += num[i] - num[i - 1]; stone[j] = 1; } } k = j + 100; for(i = 1; i <= k; i++){ f[i] = 0xFFFFFFF; for(j = s; j <= t; j++){ if(i - j < 0){ break; } f[i] = min(f[i], f[i - j] + stone[i]); } } printf("%d\n", f[k]); return 0; }