#include <bits/stdc++.h>
using namespace std;
struct union_find {
int n;
vector<int> par, size;
vector<map<int, int> > mp;
union_find(int _n) : n(_n), par(n+1), size(n+1, 1), mp(n+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);
if(mp[a].size() < mp[b].size()) swap(mp[a], mp[b]);
size[a] += size[b];
par[b] = a;
for(auto [x, y] : mp[b]) insert(-x, y);
}
void insert(int u, int v) {
int p = find(u);
auto it = mp[p].lower_bound(-u);
if(it != mp[p].end() && it->second >= v) return ;
it = mp[p].insert(it, { -u, v });
it->second = v;
while(it != mp[p].begin() && prev(it)->second <= v) mp[p].erase(prev(it));
}
int query(int u) {
int p = find(u);
auto it = mp[p].lower_bound(-u);
return it == mp[p].end() ? 0 : it->second;
}
};
signed main() {
int n, d; cin >> n >> d;
vector<int> dp(n+1, 1), a(n+1);
map<int, vector<int> > mp;
for(int i=1; i<=n; i++) {
cin >> a[i];
mp[a[i]].push_back(i);
}
union_find dsu(n);
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));
dp[p] = dsu.query(p) + 1;
st.insert(p);
}
for(int p : vec) dsu.insert(p, dp[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... |