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 int long long
#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 = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, d;
int a[maxN];
int t[maxN];
int f[maxN];
int lg[maxN], lift[21][maxN];
int lab[maxN];
vector<int> vc[maxN];
int dp[maxN];
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 Log()
{
for(int i=2; i<maxN; i++)
lg[i] = lg[i / 2] + 1;
}
void SparseTable()
{
for(int i=1; i<=n; i++) lift[0][i] = a[i];
for(int k=1; k<=lg[n]; k++)
{
for(int i=1; i<=n; i++)
{
int j = i + (1 << (k - 1));
if(j > n) lift[k][i] = lift[k - 1][i];
else lift[k][i] = min(lift[k - 1][i], lift[k - 1][j]);
}
}
}
int get(int l, int r)
{
int j = r - l + 1;
int k = lg[j];
return min(lift[k][l], lift[k][r - (1 << k) + 1]);
}
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);
}
}
// cout << t[1];return;
for(int i=1; i<=n; i++)
{
f[i] = 1;
for(int j=1; j<i; j++)
{
if(a[i] > a[j] && t[j] >= i) f[i] = max(f[i], f[j] + 1);
}
}
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);
Log();
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... |