forked from c910335/2D-Rank-Finding-Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
82 lines (70 loc) · 1.49 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int i, x, y;
} point;
int cmp(const void * a, const void * b) {
if ((*(point*)a).x == (*(point*)b).x) {
if ((*(point*)a).y < (*(point*)b).y)
return -1;
if ((*(point*)a).y == (*(point*)b).y)
return 0;
return 1;
}
if ((*(point*)a).x < (*(point*)b).x)
return -1;
return 1;
}
int n, ranks[100000];
point ps[100000];
void find_ranks(int first, int last) {
if (last - first < 2)
return;
int mid = (first + last) / 2;
find_ranks(first, mid);
find_ranks(mid, last);
int i, j = 0, k = mid, size = mid - first;
point *tps = (point*) malloc(size * sizeof(point));
for (i = 0; i != size; ++i)
tps[i] = ps[i + first];
for(i = first;; ++i) {
if (tps[j].y <= ps[k].y) {
ps[i] = tps[j++];
if (j == size) {
for (; k != last; ++k)
ranks[ps[k].i] += j;
break;
}
} else {
ps[i] = ps[k];
ranks[ps[k].i] += j;
k++;
if (k == last) {
for (++i; i != last; ++i, ++j)
ps[i] = tps[j];
break;
}
}
}
free(tps);
}
int main() {
for(;;) {
scanf("%d", &n);
if (n == 0)
return 0;
int i;
for (i = 0; i != n; ++i) {
scanf("%d%d", &ps[i].x, &ps[i].y);
ps[i].i = i;
ranks[i] = 0;
}
qsort(ps, n, sizeof(point), cmp);
find_ranks(0, n);
printf("%d", ranks[0]);
for (i = 1; i != n; ++i)
printf(" %d", ranks[i]);
puts("");
}
}