-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1016e.cpp
More file actions
56 lines (42 loc) · 1.01 KB
/
1016e.cpp
File metadata and controls
56 lines (42 loc) · 1.01 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
#include <bits/stdc++.h>
using namespace std;
bool canAchieve(const vector<int>& a, int n, int k, int x) {
vector<int> cnt(x + 1, 0);
int cur_mex = 0;
int segments = 0;
for (int i = 0; i < n; i++) {
if(a[i] <= x) cnt[a[i]]++;
while(cur_mex <= x && cnt[cur_mex] > 0) cur_mex++;
if(cur_mex >= x) {
segments++;
fill(cnt.begin(), cnt.end(), 0);
cur_mex = 0;
}
}
return segments >= k;
}
void solve() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
int l = 0, r = n + 1, ans = 0;
while(l <= r) {
int mid = (l + r) / 2;
if(canAchieve(a, n, k, mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) solve();
return 0;
}