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;
const int INF = 1e9;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N,X; cin >> N >> X;
vector<int> arr(N);
for(int i = 0;i<N;++i){
cin >> arr[i];
}
vector<int> dp(N+1,INF), L(N,1);
dp[0] = -INF; int ans = 1;
for(int i = 0;i<N;++i){
int ind = lower_bound(dp.begin(),dp.end(),arr[i])-dp.begin(); //first that is >= arr[i]
dp[ind] = arr[i];
L[i] = ind+1;
ans = max(ans,L[i]);
}
dp = vector<int>(N+1,INF);
dp[0] = -INF;
for(int i = N-1;i>=0;--i){ //1 to i have been decreased by X and we want to find the LDS from N-1 to i -> this is equivalent to increasing i by X
int ind = lower_bound(dp.begin(),dp.end(),-(arr[i]+X))-dp.begin(); //we want to find the negative version so that we can still lower bound
ans = max(ans,L[i]+ind); //no -1 because in ind, i was not counted
ind = lower_bound(dp.begin(),dp.end(),-arr[i])-dp.begin();
dp[ind] = -arr[i];
}
cout << ans << "\n";
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... |