about summary refs log tree commit diff
path: root/others/other/interval.c
blob: c068952900174290600d3d46def5f339e7322213 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
}