-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1993D.cpp
119 lines (98 loc) · 2.11 KB
/
1993D.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
117
118
119
// Created at: Sun Aug 4 23:28:46 CST 2024
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#ifdef DBG
#include "debug.h"
#else
#define dbg(...)
#define dbg_export(...)
#endif
vector<int> a;
int n, k;
void reset_globals()
{
n = 0;
k = 0;
a.clear();
}
bool canAchieveMedian(int X, [[maybe_unused]] int m, int P)
{
// pick exactly m elements, m <= k
// At least P of them is >= X
dbg(X);
int inf = INT_MIN;
int invalid = -inf;
vector<int> b(n);
for (int i = 0; i < n; i++) {
b[i] = a[i] >= X;
}
dbg(b);
// dp[i]: i is the last picked element
// for the first element, i % k == 0
vector<int> dp(n, invalid);
vector<int> pre(n, invalid);
for (int i = 0; i < n; ++i) {
int vi = b[i];
if (i % k == 0) {
dp[i] = vi;
} else {
dp[i] = max(dp[i], pre[i - 1] + vi);
}
pre[i] = max(i >= k ? pre[i - k] : invalid, dp[i]);
}
dbg(dp);
dbg(pre);
auto maxii = invalid;
for (int i = 0; i < n; i++)
if (i % k < m) {
maxii = max(maxii, dp[i]);
}
dbg(maxii, P);
return maxii >= P;
}
i64 solve()
{
int m = (n % k == 0) ? k : n % k;
/// m = 1, P = 1
/// m = 2, P = 2
/// m = 3, P = 2
/// m = 4, P = 3
/// m = 5, P = 3
int P = (m + 2) / 2;
dbg(a, k);
dbg(m, P);
int low = 1, high = *max_element(a.begin(), a.end());
int best = low;
// canAchieveMedian(9, m, P);
while (low <= high) {
int mid = low + (high - low) / 2;
if (canAchieveMedian(mid, m, P)) {
best = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return best;
}
int main()
{
#ifndef DBG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int t;
cin >> t;
while (t--) {
reset_globals();
cin >> n >> k;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
auto ans = solve();
cout << ans << endl;
}
return 0;
}
// Time Complexity: O(n log(max_element)) per test case.