-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCS_57_CeilAndFloor.cpp
58 lines (51 loc) · 1.06 KB
/
CS_57_CeilAndFloor.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
#include <bits/stdc++.h>
using namespace std;
int getFloor(vector<int> &a, int n, int x)
{
int ans = -1, low = 0, high = n - 1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (a[mid] <= x)
{
ans = a[mid];
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return ans;
}
int getCeil(vector<int> &nums, int n, int target)
{
int low = 0, high = n - 1, ans = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (nums[mid] >= target)
{
ans = nums[mid];
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return ans;
}
pair<int, int> getFloorAndCeil(vector<int> &a, int n, int x)
{
return {getFloor(a, n, x), getCeil(a, n, x)};
}
int main()
{
vector<int> a = {1, 2, 8, 10, 10, 12, 19};
int n = a.size();
int x = 5;
pair<int, int> ans = getFloorAndCeil(a, n, x);
cout << ans.first << " " << ans.second << endl;
return 0;
}