From f4f486fb4b37b72ca345f95881e44043c728b2c3 Mon Sep 17 00:00:00 2001 From: Raphael McSinyx Date: Tue, 10 Jan 2017 10:46:15 +0700 Subject: Add others/other --- others/other/interval.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 others/other/interval.c (limited to 'others/other/interval.c') diff --git a/others/other/interval.c b/others/other/interval.c new file mode 100644 index 0000000..c068952 --- /dev/null +++ b/others/other/interval.c @@ -0,0 +1,65 @@ +#include +#include + +int sign(long x) +{ + return (x) ? ((x > 0) ? 1 : -1) : x; +} + +int cmp(const void *x, const void *y) +{ + return sign(*(long *) x - *(long *) y); +} + +int CMP(const void *x, const void *y) +{ + return -cmp(x, y); +} + +int main() +{ + FILE *f = fopen("INTERVAL.INP", "r"); + short n, i, k = 0, *r; + long c, x, *a, *b; + + fscanf(f, "%hd %ld %ld", &n, &c, &x); + a = malloc(n * sizeof(long)); + b = malloc(n * sizeof(long)); + + for (i = 0; i < n; i++) + fscanf(f, "%ld %ld", a + i, b + i); + + fclose(f); + + r = calloc(n, sizeof(short)); + for (i = 0; i < n; i++) + if (a[i] <= x && b[i] >= x) + k += r[i] = 1; + + f = fopen("INTERVAL.OUT", "w"); + + if (k) { + fprintf(f, "%hd\n", k); + + for (i = 0; i < n; i++) + if (r[i]) + fprintf(f, "%hd ", i + 1); + putc(10, f); + } else { + qsort(a, n, sizeof(long), cmp); + qsort(b, n, sizeof(long), CMP); + + free(r); + r = malloc(2 * sizeof(short)); + for (i = 0; i < n && b[i] > x; i++); + r[0] = (i == n) ? -c : b[i]; + for (i = 0; i < n && a[i] < x; i++); + r[1] = (i == n) ? c : a[i]; + + fprintf(f, "0\n%hd %hd\n", r[0], r[1]); + } + + fclose(f); + + return 0; +} -- cgit 1.4.1