#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct node
{
int l = 0;
int r = 1'073'741'823;
node* left;
node* right;
int max_val = 0;
bool is_left = false;
bool is_right = false;
void uleft()
{
if(!is_left)
{
left = new node;
left -> l = l;
left -> r = (l+r)/2;
}
is_left = true;
}
void uright()
{
if(!is_right)
{
right = new node;
right -> l = (l+r)/2+1;
right -> r = r;
}
is_right = true;
}
int get_max(int p1, int p2)
{
if(p1 > p2) return 0;
if(r < p1 || l > p2) return 0;
if(l >= p1 && r <= p2) return max_val;
if(p1 <= (l+r)/2)
{
uleft();
}
if(p2 >= (l+r)/2+1)
{
uright();
}
return max((is_left ? left->get_max(p1,p2) : 0),(is_right ? right->get_max(p1,p2) : 0));
}
void upd_max(int p, int val)
{
if(l == r)
{
if(val == -1)
{
max_val = 0;
}
max_val = max(max_val,val);
return;
}
if(p <= (l+r)/2)
{
uleft();
left -> upd_max(p,val);
}
else
{
uright();
right -> upd_max(p,val);
}
max_val = 0;
if(is_left)
{
max_val = max(max_val,left -> max_val);
}
if(is_right)
{
max_val = max(max_val,right -> max_val);
}
}
};
ll arr[200'001];
int max_pref[200'001];
int max_suf[200'001];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
int n;
ll x;
cin >> n >> x;
rep(i,n) cin >> arr[i];
node tree;
rep(i,n)
{
int val = tree.get_max(0,arr[i]-1)+1;
max_pref[i] = val;
tree.upd_max(arr[i],val);
}
rep(i,n) tree.upd_max(arr[i],-1);
for(int i = n-1; i >= 0; i--)
{
int val = tree.get_max(arr[i]+1,1e9)+1;
max_suf[i] = val;
tree.upd_max(arr[i],val);
}
rep(i,n) tree.upd_max(arr[i],-1);
int ans = 0;
for(int i = n-1; i >= 0; i--)
{
ans = max(ans,max_pref[i] + tree.get_max(max(0LL,arr[i]-x+1),1e9+1));
tree.upd_max(arr[i],max_suf[i]);
}
rep(i,n) tree.upd_max(arr[i],-1);
rep(i,n)
{
ans = max(ans,max_suf[i] + tree.get_max(0,max(0LL,arr[i]-x-1)));
tree.upd_max(arr[i],max_pref[i]);
}
cout << ans << "\n";
}
# | 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... |