#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define sz size()
#define all(v) v.begin(),v.end()
#define fi first
#define se second
const int N = 2e5;
const int mod = 1e9+7;
const int INF = 1e9+10;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
int calc(vector<int>& b,int n){
vector<int> d(n+1,INF);
d[0]=-INF;
for(int i = 0; i < n; i++){
int l=upper_bound(all(d),b[i])-d.begin();
if(d[l-1]<b[i] && b[i]<d[l])
d[l]=b[i];
}
int ans=0;
for(int i = 0; i <= n; i++){
if(d[i]<INF)
ans=i;
}
for(int i = 0; i <= n; i++){
cout<<d[i]<<" ";
}
cout<<"\n";
return ans;
}
void solve() {
int n,d;
cin>>n>>d;
int a[n];
for(int i = 0; i < n; i++){
cin>>a[i];
}
int X[n],Y[n],ans=0;
memset(X,0,sizeof(X));
memset(Y,0,sizeof(Y));
vector<int> dp(n+1,INF);
for(int i = 0; i < n; i++){
int j=lower_bound(all(dp),a[i])-dp.begin();
dp[j]=a[i];
X[i]=j+1;
ans=max(ans,X[i]);
}
vector<int> dp2(n+1,INF);
for(int i = n-1; i >= 0; i--){
int j=lower_bound(all(dp2),-(a[i]-d))-dp2.begin();
Y[i]=j+1;
dp2[lower_bound(all(dp2),-a[i])-dp2.begin()]=-a[i];
}
for(int i = 0; i < n; i++){
ans=max(ans,X[i]+Y[i]-1);
}
cout<<ans<<"\n";
}
int main() {
//freopen("cowrun.in","r",stdin);
//freopen("cowrun.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
//cin>>t;
while(t--) {
solve();
}
return 0;
}
# | 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... |