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>
using namespace std;
#define pb push_back
#define int long long
#define F first
#define S second
const int inf = 1e14;
const int N = 2e5+3;
int dp[N], ndp[N];
pair<int, int> mdp[N];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, d;
cin >> n >> d;
int a[n];
for(int i=0; i< n; i++)cin >> a[i];
dp[0] = inf;
int mx=0;
for(int i = n-1; i>=0; i--){
if(a[i]<dp[mx]){
mx++;
dp[mx]=a[i];
mdp[i] = {mx, dp[mx]};
}
else{
int l = 1, r = mx;
while(l<=r){
int s = l+r>>1;
if(dp[s]>=a[i])l=s+1;
else r=s-1;
}
if(dp[l-1]!=a[i])dp[l]=max(dp[l], a[i]);
mdp[i] = {l, dp[l]};
}
}
int ans=mx;
for(int i = 1; i<=n; i++)ndp[i]=inf+i;
ndp[0]=-inf;
int mxx=0;
for(int i=0; i < n; i++){
if(a[i]-d>ndp[mxx]){
mxx++;
ndp[mxx]=a[i]-d;
}
else{
int l = 1, r = n;
while(l<=r){
int s = l+r>>1;
if(ndp[s]>=a[i]-d)r=s-1;
else l=s+1;
}
if(dp[l-1]!=a[i]-d)ndp[l]=min(ndp[l], a[i]-d);
}
int l = 0, r = n;
while(l<=r){
int s = l+r>>1;
if(ndp[s]<mdp[i+1].S)l=s+1;
else r=s-1;
}
ans=max(ans, mdp[i+1].F+r);
}
cout << ans;
}
Compilation message (stderr)
glo.cpp: In function 'int main()':
glo.cpp:34:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int s = l+r>>1;
| ~^~
glo.cpp:54:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int s = l+r>>1;
| ~^~
glo.cpp:62:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int s = l+r>>1;
| ~^~
# | 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... |