-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathternary_search.cpp
56 lines (43 loc) · 958 Bytes
/
ternary_search.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
#include <bits/stdc++.h>
using namespace std;
// Calculates the cost at the given point
// Must be a parabolic function matching the desired extreme (min or max)
// of the ternary search function
double calc(double m) {
// TODO: to be implemented
return 1;
}
// Ternary search on to find a real (floating) local minimum
double ternarySearchDouble() {
double l = 0, r = 1e18;
for (int i = 0; i < 1000; ++i) {
double mid1 = l + (r - l) / 3;
double mid2 = r - (r - l) / 3;
if (calc(mid1) < calc(mid2))
r = mid2;
else
l = mid1;
}
return l;
}
// Ternary search to find an interger local minimum
int ternarySearchInt() {
int l = 0, r = 1e6;
while (r - l > 10) {
int mid1 = l + (r - l) / 3;
int mid2 = r - (r - l) / 3;
if (calc(mid1) < calc(mid2))
r = mid2;
else
l = mid1;
}
int pos, val = 1e9;
for (int i = l; i <= r; ++i) {
int tmp = calc(i);
if (val > tmp) {
val = tmp;
pos = i;
}
}
return pos;
}