-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.cpp
116 lines (99 loc) · 3.27 KB
/
binary_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
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
108
109
110
111
112
113
114
115
116
#include <algorithm>
#include <concepts>
#include <iostream>
#include <vector>
// [T, T, T, ..., T, F, F, F, ...]
// find the first position p in [l, r] such that condition(p) is false
#include <optional>
#include <ranges>
enum class BoundType { Lower, Upper };
enum class TargetPosition { First, Last };
std::optional<long long> FindBound(long long l, long long r,
const auto& condition, BoundType bound_type,
TargetPosition target_position) {
if (r < l) {
return std::nullopt;
}
auto range = std::views::iota(l, r + 1);
auto iter =
(target_position == TargetPosition::First ? range.end() : range.begin());
bool is_upper_bound = (target_position == TargetPosition::Last &&
bound_type == BoundType::Lower) ||
(target_position == TargetPosition::First &&
bound_type == BoundType::Upper);
auto func = [is_upper_bound](auto&&... args) {
if (is_upper_bound)
return std::ranges::upper_bound(std::forward<decltype(args)>(args)...);
else
return std::ranges::lower_bound(std::forward<decltype(args)>(args)...);
};
auto reversed_condition = [&](long long x) {
return target_position == TargetPosition::Last ? !condition(x)
: condition(x);
};
auto comparator = [&](auto a, auto b) {
auto x = is_upper_bound ? b : a;
return reversed_condition(x);
};
iter = func(range, 0, comparator);
if (target_position == TargetPosition::Last && iter != range.begin()) {
--iter;
return *iter;
}
if (target_position == TargetPosition::First && iter != range.end()) {
return *iter;
}
return std::nullopt;
}
std::optional<long long> FirstFalse(long long l, long long r,
const auto& condition) {
return FindBound(l, r, condition, BoundType::Lower, TargetPosition::First);
}
std::optional<long long> FirstTrue(long long l, long long r,
const auto& condition) {
return FindBound(l, r, condition, BoundType::Upper, TargetPosition::First);
}
std::optional<long long> LastFalse(long long l, long long r,
const auto& condition) {
return FindBound(l, r, condition, BoundType::Upper, TargetPosition::Last);
}
std::optional<long long> LastTrue(long long l, long long r,
const auto& condition) {
return FindBound(l, r, condition, BoundType::Lower, TargetPosition::Last);
}
using namespace std;
int main() {
vector<int> f = {1, 2, 3, 4, 6};
{
auto result = FirstTrue(0, 4, [&](int pos) { return f[pos] > 3; });
if (result) {
cout << *result << endl;
} else {
cout << -1 << endl;
}
}
{
auto result = FirstTrue(0, 4, [&](int pos) { return f[pos] > 10; });
if (result) {
cout << *result << endl;
} else {
cout << -1 << endl;
}
}
{
auto result = FirstFalse(0, 4, [&](int pos) { return f[pos] <= 1; });
if (result) {
cout << *result << endl;
} else {
cout << -1 << endl;
}
}
{
auto result = FirstFalse(0, 4, [&](int pos) { return f[pos] > 0; });
if (result) {
cout << *result << endl;
} else {
cout << -1 << endl;
}
}
}