This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pii pair<int,int>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define f first
#define int long long
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
	tree_order_statistics_node_update> ordered_set;
const int N = 1e6 + 5	;
const int inf = 1e17 + 7;
const int mod = 1e9 + 7;
int n,m,k;
void solve(){
	
	cin >> n >> m;
	
	vector<int>a(n + 1);
	
	for(int i = 0;i<n;i++){
		cin >> a[i];
	}
	
	vector<int>dp(n + 1),v(n + 1,-inf);
	
	for(int i = n - 1;i>=0;i--){
		int it = upper_bound(all(v),a[i]) - v.begin() - 1;
		v[it] = a[i];
		if(i > 0){
			dp[i - 1] = v.end() - upper_bound(all(v), a[i - 1] - m);
		}
	}
	
	int ans = v.end() - upper_bound(all(v), -inf + 1);
	
	for(int i = 0;i<n;i++){
		v[i] = inf;
	}
	
	for(int i = 0;i<n - 1;i++){
		int it = lower_bound(all(v), a[i]) - v.begin();
		v[it] = a[i];
		umax(ans, dp[i] + it + 1);
	}
	
	cout<< ans <<"\n";
	
}
signed main()
{
	
//	freopen("seq.in", "r", stdin);
//  freopen("seq.out", "w", stdout);
	ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
	int tt=1;//cin>>tt;
	while(tt--)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... |