#include <bits/stdc++.h>
#define ar array
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
struct union_find {
int n;
vector<int> par, size;
union_find(int _n) : n(_n), par(n+1), size(n+1, 1) {
for(int i=1; i<=n; i++) par[i] = i;
}
int find(int u) {
if(u == par[u]) return u;
return par[u] = find(par[u]);
}
void uni(int a, int b) {
a = find(a); b = find(b);
if(a == b) return ;
if(size[a] < size[b]) swap(a, b);
size[a] += size[b];
par[b] = a;
}
};
signed main() {
int n, d; cin >> n >> d;
vector<int> dp(n+1, 1);
map<int, vector<int> > mp;
for(int i=1; i<=n; i++) {
int x; cin >> x;
mp[x].push_back(i);
}
union_find dsu(n);
vector<int> act(n+1);
set<int> st;
for(auto [val, vec] : mp) {
for(int p : vec) {
if(!st.empty() && *st.begin() < p && p - *--st.upper_bound(p) <= d) dsu.uni(p, *--st.upper_bound(p));
if(!st.empty() && *st.rbegin() > p && *st.lower_bound(p) - p <= d) dsu.uni(p, *st.lower_bound(p));
for(int i=1; i<p; i++) if(dsu.find(i) == dsu.find(p)) dp[p] = max(dp[p], dp[i] + 1);
}
for(int p : vec) {
st.insert(p);
}
}
cout << *max_element(dp.begin(), dp.end()) << '\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... |