-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprob8-2.c
69 lines (54 loc) · 1.66 KB
/
prob8-2.c
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
66
67
68
69
/*
file: prob8-2.c
author: David De Potter
description: extra, problem 8, segments
version: 8.2, using the clib library and qsort
*/
#include "../../Functions/clib/clib.h"
typedef struct {
int start, end;
} Seg;
int compareSegs (const void *a, const void *b) {
/* compares two segments by their start field */
return ((Seg *)a)->start - ((Seg *)b)->start;
}
Seg* readInput (int len) {
/* reads the input and stores it as an array of segments */
Seg *segments = safeMalloc(len*sizeof(Seg));
for (int i = 0; i < len; ++i)
(void)! scanf("[%d,%d),", &segments[i].start, &segments[i].end);
return segments;
}
void printSegments (Seg *segments, int n) {
/* prints the segments in the array */
for (int i = 0; i <= n; ++i) {
printf("[%d,%d)", segments[i].start, segments[i].end);
printf(i == n ? "\n" : ",");
}
}
void mergeSegments(Seg *segments, int n) {
/* checks each segment pair and keeps merging them as long
* as the value of the end field of the current segment
* overlaps the start field's value of the next */
int curr = 0; // index of current segment in the merged (sub)array
for (int i = 1; i < n; ++i) {
Seg a = segments[curr], b = segments[i];
if (a.end >= b.start) {
// merge segments
segments[curr].start = MIN (a.start, b.start);
segments[curr].end = MAX (a.end, b.end);
} else segments[++curr] = b;
}
printSegments (segments, curr);
}
int main() {
int n;
(void)! scanf("%d:", &n);
Seg *segments = readInput (n);
// sort the segments
qsort(segments, n, sizeof(Seg), compareSegs);
// merge the segments
mergeSegments(segments, n);
free(segments);
return 0;
}