-
Notifications
You must be signed in to change notification settings - Fork 1
/
P10012.cpp
94 lines (84 loc) · 2.1 KB
/
P10012.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
#define RADIUS second
struct PermutationNode {
PermutationNode *next;
double radius;
};
struct PermutationHandler {
PermutationNode *nodes;
PermutationHandler(int size, double *radii) {
nodes = new PermutationNode[size+1];
for(int i = 0; i < size; ++i) {
nodes[i+1].radius = radii[i];
nodes[i].next = &(nodes[i+1]);
}
nodes[size].next = NULL;
}
~PermutationHandler() {
delete[] nodes;
}
PermutationNode* root() {
return &(nodes[0]);
}
};
// oO
double delta(double r1, double r2) {
if(r1 > r2)
swap(r1, r2);
double R = r1+r2;
double r = r2-r1;
return sqrt(R*R-r*r);
}
void findOptimalPacking(const int i, const int N, PD *perm, double &bestWidth, PermutationHandler &ph) {
if(i == N) {
double maxWidth = 0;
FORJ(N) {
double candidate = perm[j].XX + perm[j].RADIUS;
maxWidth = MAX(maxWidth, candidate);
}
/*if(maxWidth < bestWidth) {
cerr << "Found better packing: " << maxWidth << endl;
FORJ(N) {
cerr << " r=" << perm[j].RADIUS << "@" << perm[j].XX;
}
cerr << endl;
}//*/
bestWidth = MIN(maxWidth, bestWidth);
return;
}
// try all combinations:
PermutationNode *node = ph.root();
while(node->next != NULL) {
PermutationNode *n = node->next;
// remove n from permutation:
node->next = n->next;
double newRadius = n->radius;
double newX = newRadius;
FORJ(i) {
double xx = perm[j].XX + delta(perm[j].RADIUS, newRadius);
newX = MAX(xx, newX);
}
perm[i].XX = newX;
perm[i].RADIUS = newRadius;
findOptimalPacking(i+1, N, perm, bestWidth, ph);
// re-insert in permutation and go to next:
node->next = n; // n->next is already set (never changes)
node = n;
}
}
int main() {
PD perm[10];
double radii[10];
FORCAS {
int N; cin >> N;
double maxRadius = 0;
FORI(N) {
cin >> radii[i];
maxRadius = MAX(maxRadius, radii[i]);
}
PermutationHandler ph(N, radii);
double bestWidth = 2*maxRadius*N;
findOptimalPacking(0, N, perm, bestWidth, ph);
printf("%.3lf\n", bestWidth);
}
return 0;
}