-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
76 lines (54 loc) · 1.06 KB
/
binary_search.cpp
File metadata and controls
76 lines (54 loc) · 1.06 KB
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
bool check(int q){
// check function;
}
/// TTTTTTTTTTFFFFFFFF
int l=-1;
int r=k+1;
// k+1 dima False (invariant;
// l=-1 tnejem tbadelha ama nejmou nhotoha =0 mathalan el
// lehné khtarna =-1 bech ken matbadletech naarfou eli mafama hata solution !
while(l<r){
int mid=l+(r-l+1)/2;
if(check(mid)){
//cout << mid << endl;
l=mid;
}else{
r=mid-1;
}
}
// another implem from usaco :
int last_true(int lo, int hi) {
lo--;
while (lo < hi) {
int mid = lo + (hi - lo + 1) / 2;
if (check(mid)) {
lo = mid;
} else {
hi = mid - 1;
}
}
return lo;
}
///FFFFFFFTTTTTTTT
int l=0; // faux;
int r=n; // vrai >> invariant :)
while(l<r){
int mid=l+(r-l)/2;
if(check(mid)){
r=mid;
}else{
l=mid+1;
}
}
cout << r << "\n";
////////////////////////
binary search on real values:
const double EPS = 1e-7;
bool check(double r){}
double lo = 0.0, hi = 10000000000000000.0;
while (fabs(hi-lo) > EPS) {
// answer is not found yet
double mid = (lo+hi) / 2.0;
// try the middle value
check(mid) ? hi = mid : lo = mid;
}