#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false
const int MAXN = 300 * 1000 + 17;
int a[MAXN];
vector<int> v;
multiset<int> ms[MAXN];
int st[4 * MAXN];
int dp[MAXN];
void aktualizuj(int p, int k, int ind, int w, int i) {
if (p == k && p == ind) {
st[i] = w;
return;
}
int sr = (p + k) / 2;
if (ind <= sr) {
aktualizuj(p, sr, ind, w, i * 2);
} else {
aktualizuj(sr + 1, k, ind, w, i * 2 + 1);
}
st[i] = max(st[i * 2], st[i * 2 + 1]);
}
int zapytanie(int p, int k, int a, int b, int i) {
if (p > b || k < a) {
return 0;
}
if (a <= p && k <= b) {
return st[i];
}
int sr = (p + k) / 2;
return max(zapytanie(p, sr, a, b, i * 2), zapytanie(sr + 1, k, a, b, i * 2 + 1));
}
int main () {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i ++) {
cin >> a[i];
}
for (int i = 0; i < n; i ++) {
v.pb(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int m = int(v.size());
for (int i = 0; i < n; i ++) {
a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
}
for (int i = 1; i <= m; i ++) {
ms[i].insert(0);
}
int wyn = 0;
for (int i = 0; i < n; i ++) {
int x = a[i];
dp[i] = zapytanie(0, m, 0, x - 1, 1) + 1;
ms[x].insert(dp[i]);
aktualizuj(0, m, x, *ms[x].rbegin(), 1);
wyn = max(wyn, dp[i]);
if (i - d >= 0) {
int x = a[i - d];
ms[x].erase(ms[x].find(dp[i - d]));
aktualizuj(0, m, x, *ms[x].rbegin(), 1);
}
}
cout << wyn << "\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... |