This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Make the best become better
// No room liftor laziness
#include<bits/stdc++.h>
#define pb push_back
#define lifti liftirst
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 3e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e9;
int n, d;
int a[maxN];
int t[maxN];
int f[maxN];
int lab[maxN];
vector<int> vc[maxN];
int dp[maxN];
int st[4 * maxN], lazy[4 * maxN];
void down(int id)
{
int t = lazy[id];
st[id * 2] = max(st[id * 2], t);
st[id * 2 + 1] = max(st[id * 2 + 1], t);
lazy[id * 2] = max(lazy[id * 2], t);
lazy[id * 2 + 1] = max(lazy[id * 2 + 1], t);
}
void update(int id, int l, int r, int u, int v, int val)
{
if(l > v || r < u) return;
if(l >= u && r <= v)
{
st[id] = max(st[id], val);
lazy[id] = max(lazy[id], val);
return;
}
down(id);
int mid = (l + r) / 2;
update(id * 2, l, mid, u, v, val);
update(id * 2 + 1, mid + 1, r, u, v, val);
}
int get(int id, int l, int r, int pos)
{
if(l == r) return st[id];
int mid = (l + r) / 2;
down(id);
if(pos <= mid) return get(id * 2, l, mid, pos);
return get(id * 2 + 1, mid + 1, r, pos);
}
int Findset(int u)
{
return lab[u] < 0 ? u : lab[u] = Findset(lab[u]);
}
void Unite(int u, int v)
{
int r = Findset(u), s = Findset(v);
if(r == s) return;
if(lab[r] > lab[s]) swap(r, s);
dp[r] = max(dp[r], dp[s]);
lab[r] += lab[s];
lab[s] = r;
}
void ReadInput()
{
vector<int> vc;
cin >> n >> d;
for(int i=1; i<=n; i++)
{
cin >> a[i];
vc.pb(a[i]);
}
sort(vc.begin(), vc.end());
vc.erase(unique(vc.begin(), vc.end()), vc.end());
for(int i=1; i<=n; i++)
a[i] = upper_bound(vc.begin(), vc.end(), a[i]) - vc.begin();
}
void Solve()
{
memset(lab, -1, sizeof lab);
for(int i=1; i<=n; i++)
vc[a[i]].pb(i);
for(int i=1; i<=n; i++)
dp[i] = i;
set<int> S;
for(int val=1; val<=n; val++)
{
for(int v : vc[val])
{
auto it = S.lower_bound(v - d);
if(it != S.end() && abs(v - *it) <= d) Unite(*it, v);
it = S.upper_bound(v + d);
if(it != S.begin())
{
it--;
if(abs(v - *it) <= d) Unite(*it, v);
}
S.insert(v);
}
for(int v : vc[val])
{
t[v] = min(n, dp[Findset(v)] + d);
}
}
for(int val=1; val<=n; val++)
{
for(int v : vc[val])
f[v] = get(1, 1, n, v) + 1;
for(int v : vc[val])
{
update(1, 1, n, v + 1, t[v], f[v]);
}
}
cout << *max_element(f + 1, f + n + 1);
}
int32_t main()
{
// freopen("sol.inp", "r", stdin);
// freopen("sol.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
// 2 3 8 5 8 7 8 10 10 10
# | 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... |