#include <bits/stdc++.h>
using namespace std;
int bitmask(int n, int d, vector<int> a) {
int ans = 0;
for (int mask = 1; mask < (1 << n); mask++) {
vector<int> active; for (int i = 0; i < n; i++) if (mask & (1 << i)) active.push_back(i);
int calc = 1, cmax = a[active[0]];
for (int i = 1; i < active.size(); i++) {
if (active[i] - active[i-1] > d) {
calc = 0; break;
}
if (a[active[i]] > cmax) {
cmax = a[active[i]];
calc++;
}
}
ans = max(ans, calc);
}
return ans;
}
int dp(int n, int d, vector<int> a) {
vector<vector<bool>> reachable(n, vector<bool>(n, false));
for (int i = 0; i < n; i++) {
reachable[i][i] = true;
int max_dist = 0, prev_se = i;
for (int j = i+1; j < n; j++) {
max_dist = max(max_dist, j - prev_se);
if (a[j] <= a[i]) prev_se = j;
if (max_dist > d) break;
reachable[i][j] = true;
}
}
vector<int> DP(n, 0);
for (int i = 0; i < n; i++) {
DP[i] = 1;
for (int j = i - 1; j >= 0; j--) {
if (a[j] >= a[i]) continue;
if (reachable[j][i]) DP[i] = max(DP[i], DP[j] + 1);
}
}
return *max_element(DP.begin(), DP.end());
}
void debug() {
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution dist(0, 5);
for (int TRIAL = 0; TRIAL < 1'000; TRIAL++) {
int N = 6; vector<int> INPUT; for (int i = 0; i < N; i++) INPUT.push_back(dist(rng));
for (int D = 1; D <= N; D++) {
int bm = bitmask(N, D, INPUT);
int dpa = dp(N, D, INPUT);
if (bm != dpa) {
cout << "Failure on N: " << N << " D: " << D << " and array:"; for (auto &el : INPUT) cout << " " << el; cout << "\n";
cout << "Bitmask: " << bm << " DP: " << dpa << "\n";
break;
}
}
}
}
int main() {
// debug();
cin.tie(0)->sync_with_stdio(0);
int n, d; cin >> n >> d;
vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
cout << dp(n, d, a) << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |