diff options
author | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2020-06-06 21:33:13 +0700 |
---|---|---|
committer | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2020-06-06 21:33:13 +0700 |
commit | 2f674dc80f0382f1c3178f435714960734dc9d3c (patch) | |
tree | 2abba7e4ec72bd16f58f7375126144d3fd9f4bca /09/TP-HN-2014/cau4.c | |
parent | b2d80610db6beda38573890ed169815e495bc663 (diff) | |
download | cp-2f674dc80f0382f1c3178f435714960734dc9d3c.tar.gz |
Reorganize stuff from secondary school
Diffstat (limited to '09/TP-HN-2014/cau4.c')
-rw-r--r-- | 09/TP-HN-2014/cau4.c | 48 |
1 files changed, 0 insertions, 48 deletions
diff --git a/09/TP-HN-2014/cau4.c b/09/TP-HN-2014/cau4.c deleted file mode 100644 index 49e3df6..0000000 --- a/09/TP-HN-2014/cau4.c +++ /dev/null @@ -1,48 +0,0 @@ -#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; -} |