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;
int main(){
int n, x;
cin >> n >> x;
int a[n];
for(int i = 0;i < n; i++) cin >> a[i];
int prefLongest[n];
vector<int> dp;
for(int i = 0; i < n; i++){
int idx = lower_bound(dp.begin(), dp.end(), a[i]) - dp.begin();
if(idx == (int)dp.size()){
dp.push_back(a[i]);
prefLongest[i] = (int)dp.size();
}
else{
dp[idx] = a[i];
prefLongest[i] = idx + 1;
}
}
deque<int> dp2;
int ans = 0;
for(int i = n - 1; i >= 0; i--){
int idx = upper_bound(dp2.begin(), dp2.end(), a[i] - x) - dp2.begin();
idx--;
// cout << (int)dp2.size() - idx - 1 << "\n";
ans = max(ans, (int)dp2.size() - idx - 1 + prefLongest[i]);
int idx2 = upper_bound(dp2.begin(), dp2.end(), a[i]) - dp2.begin();
idx2--;
if(idx2 == -1){
dp2.push_front(a[i]);
}
else{
dp2[idx2] = a[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... |