forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10797.cpp
107 lines (100 loc) · 1.63 KB
/
10797.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;
struct Point
{
double x, y;
int id;
Point *match;
};
double cross(Point *o, Point *a, Point *b)
{
return (a->x - o->x) * (b->y - o->y) - (a->y - o->y) * (b->x - o->x);
}
Point *findMatch(Point *one, Point **P, int n)
{
int a = 0, b = n - 1, i, j;
int mid = n / 2;
Point *two;
while (a < b)
{
swap(P[a], P[(a + b) >> 1]);
two = P[a];
for (i = a, j = a + 1; j <= b; j++)
{
if (cross(one, two, P[j]) > 0)
{
i++;
swap(P[i], P[j]);
}
}
swap(P[a], P[i]);
// P[0..i-1] < P[i] < P[i+1..M-1]
if (i < mid)
{
a = i + 1;
}
else if (i == mid)
{
break;
}
else
{
b = i - 1;
}
}
return P[mid];
}
Point all[10010];
Point *A[10010], *B[10010];
int N, M;
pair<int, int> solve()
{
assert(N % 2 == 1 && M % 2 == 1);
for (int i = 1; i < N; i++)
{
swap(A[i], A[rand() % (i + 1)]);
}
for (int i = 1; i < M; i++)
{
swap(B[i], B[rand() % (i + 1)]);
}
for (int i = 0; i < N; i++)
{
A[i]->match = findMatch(A[i], B, M);
}
for (int i = 0; i < M; i++)
{
B[i]->match = findMatch(B[i], A, N);
if (B[i]->match->match == B[i])
{
return make_pair(B[i]->match->id, B[i]->id);
}
}
abort();
}
int main()
{
int total;
srand(10852765);
for (int cs = 1; scanf("%d", &total) == 1 && total > 0; cs++)
{
N = M = 0;
for (int i = 0; i < total; i++)
{
scanf("%lf %lf", &all[i].x, &all[i].y);
all[i].match = NULL;
if (all[i].x < 0)
{
all[i].id = N;
A[N++] = &all[i];
}
else
{
all[i].id = M;
B[M++] = &all[i];
}
}
pair<int, int> res = solve();
printf("Case %d: %d %d\n", cs, res.first, res.second);
}
}