about summary refs log tree commit diff
path: root/others/other/interval.c
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2017-01-10 10:46:15 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2017-01-10 10:46:15 +0700
commitf4f486fb4b37b72ca345f95881e44043c728b2c3 (patch)
tree975b9bb907903269fac982792f983d81afc65187 /others/other/interval.c
parent318184dbda0658d8afae6dd3c13a0718382726a1 (diff)
downloadcp-f4f486fb4b37b72ca345f95881e44043c728b2c3.tar.gz
Add others/other
Diffstat (limited to 'others/other/interval.c')
-rw-r--r--others/other/interval.c65
1 files changed, 65 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+
+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;
+}