-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1370D.cpp
More file actions
executable file
·47 lines (40 loc) · 766 Bytes
/
1370D.cpp
File metadata and controls
executable file
·47 lines (40 loc) · 766 Bytes
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
// Problem Code: 1370D
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int odd_even_subsequence(int n, int k, vector<int>& a) {
int mid, low = 0, high = *max_element(a.begin(), a.end());
// helper function
auto good = [&](int x) -> bool {
for (int turn: {0, 1}) {
int len = 0;
for (int& num: a)
if (!turn || num <= x) {
len++;
turn = !turn;
}
if (len >= k)
return true;
}
return false;
};
// binary search
while (low < high) {
mid = (low + high) / 2;
if (good(mid))
high = mid;
else
low = mid + 1;
}
return high;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
cout << odd_even_subsequence(n, k, a);
return 0;
}