#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define in insert
#define lb lower_bound
#define F first
#define S second
#define sz size()
#define all(v) v.begin(),v.end()
#define FOR1(x, n) for(int j = x; j <= n; j ++)
#define FOR(x, n, m, d) for(int x = n; x <= m; x += d)
#define FORR(x, n, m, d) for(int x = n; x >= m; x -= d)
#define nikita ios_base::sync_with_stdio(0), cin.tie(0);
const int N = 3e5+5;
int a[N], b[N], pref[N], dp[N], c[N], timer = 1;
int n,m,k,sum=0,x,y, ans, r, cnt, l, mod = 1e9+7;
string s, str;
struct tt{
int val, l, r;
}t[N*25][2];
void upd(int pos, int f, int val, int v = 1, int tl = 1, int tr = 1e9){
if(tl == tr){
t[v][f].val = max( t[v][f].val, val );
return;
}
int tm = (tl + tr) >> 1;
if(pos <= tm){
if(!t[v][f].l)t[v][f].l = ++timer;
upd(pos, f, val, t[v][f].l, tl, tm);
}
else{
if(!t[v][f].r)t[v][f].r = ++timer;
upd(pos, f, val, t[v][f].r, tm+1, tr);
}
t[v][f].val = max(t[t[v][f].l][f].val, t[t[v][f].r][f].val);
}
int get(int r, int f, int v = 1, int tl = 1, int tr = 1e9){
if(tr <= r){
return t[v][f].val;
}
if(tl > r)return 0;
int tm = (tl + tr) >> 1;
int x = y = 0;
if(t[v][f].l)x = get(r, f, t[v][f].l, tl, tm);
if(t[v][f].r)y = get(r, f, t[v][f].r, tm+1, tr);
return max(x, y);
}
void solve(){
cin >> n >> m;
FOR(i, 1, n, 1){
cin >> a[i];
}
upd(a[1], 0, 1);
ans = 1;
FOR(i, 2, n, 1){
dp[i] = get(min((int)1e9, a[i] + m - 1), 0);
dp[i] = max(dp[i], get(a[i]-1, 1));
ans = max( ans, dp[i] + 1 );
x = get(a[i] - 1, 0) + 1;
upd(a[i], 0, x);
upd(a[i], 1, dp[i] + 1);
}
cout << ans;
}
signed main(){
nikita
int tt = 1;
if(!tt)cin >> tt;
FOR(i, 1, tt, 1)solve();
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |