-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireFly.cpp
More file actions
72 lines (63 loc) · 1.21 KB
/
Copy pathFireFly.cpp
File metadata and controls
72 lines (63 loc) · 1.21 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
65
66
67
68
69
70
71
72
// Baekjoon 3020
// Binary Search
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, H, input, l_count, sum, ans;
vector<int>Top;
vector<int>Bottom;
int Top_search(int left, int right, int high) {
int mid, answer{ N/2 };
while (left <= right) {
mid = (left + right) / 2;
if (Top[mid] < high) {
left = mid + 1;
}
else {
right = mid - 1;
if (answer > mid)
answer = mid;
}
}
return (N/2)-answer;
}
int Bot_search(int left, int right, int high) {
int mid, answer{ N/2 };
while (left <= right) {
mid = (left + right) / 2;
if (Bottom[mid] < high) {
left = mid + 1;
}
else {
right = mid - 1;
if (answer > mid)
answer = mid;
}
}
return (N / 2) - answer;
}
int main() {
cin >> N >> H;
for (int i{ 0 }; i < N; i++) {
cin >> input;
if (i % 2 == 0) {
Bottom.push_back(input);
}
else
Top.push_back(input);
}
sort(Bottom.begin(), Bottom.end());
sort(Top.begin(), Top.end());
ans = N + 1;
for (int i{ 1 }; i <= H; i++) {
sum = Top_search(0, (N / 2) - 1, H-i +1) + Bot_search(0, (N / 2) - 1, i);
if (ans > sum) {
ans = sum;
l_count = 1;
}
else if (ans == sum)
l_count++;
}
cout << ans << " " << l_count << endl;
}