diff options
Diffstat (limited to '2ndary/09/TP-HN-2014/cau4.c')
-rw-r--r-- | 2ndary/09/TP-HN-2014/cau4.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/2ndary/09/TP-HN-2014/cau4.c b/2ndary/09/TP-HN-2014/cau4.c new file mode 100644 index 0000000..49e3df6 --- /dev/null +++ b/2ndary/09/TP-HN-2014/cau4.c @@ -0,0 +1,48 @@ +#include <stdlib.h> +#include <stdio.h> + +int cmp(const void *x, const void *y) +{ + return *(int *) x - *(int *) y; +} + +int main() +{ + FILE *f = fopen("CAU4.INP", "r"); + int n, d, k; + fscanf(f, "%d %d", &n, &d); + int *a = (int *) malloc(n * sizeof(int)); + for (k = 0; k < n; k++) + fscanf(f, "%d", a + k); + k = a[--d]; + fclose(f); + + qsort(a, n, sizeof(int), cmp); + int t = 0; + for (int i = 1; i < n; i++) + t += abs(a[i] - a[i - 1]); + int idx = (int *) bsearch(&k, a, n, sizeof(int), cmp) - a; + if ((idx - d) * (idx - n + d + 1)) { + int t0, t1; + if (idx < d) { + t0 = (t - abs(a[n - d + idx] - a[n - d + idx - 1]) + + abs(a[n - d + idx] - *a)); + d = n - d - 1; + t1 = (t - abs(a[idx - d] - a[idx - d - 1]) + + abs(a[n - 1] - a[idx - d - 1])); + } else { + t0 = (t - abs(a[idx - d] - a[idx - d - 1]) + + abs(a[n - 1] - a[idx - d - 1])); + d = n - d - 1; + t1 = (t - abs(a[n - d + idx] - a[n - d + idx - 1]) + + abs(a[n - d + idx] - *a)); + } + t = (t0 < t1) ? t0 : t1; + } + + f = fopen("CAU4.OUT", "w"); + fprintf(f, "%d\n", t); + fclose(f); + + return 0; +} |