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 ;
using ll = long long ;
const int N = 2e5 + 5 ;
int n , x , a[N], l[N], r[N] ;
int findLIS() {
vector<int> d ;
for (int i = 1 ; i <= n ; i++) {
auto it = upper_bound(d.begin(), d.end(), a[i]) ;
if (it == d.end()) d.push_back(a[i]) ;
else *it = a[i] ;
}
return d.size() ;
}
int32_t main() {
ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
cin >> n >> x ;
for (int i = 1 ; i <= n ; i++) {
cin >> a[i] ;
}
for (int i = 1 ; i <= n ; i++) {
l[i] = a[i] - x ;
r[i] = a[i] + x ;
}
int ret = findLIS() ;
if (!x) {
cout << ret ;
return 0 ;
}
vector<int> dp ;
for (int i = 1 ; i <= n ; i++) {
auto it = lower_bound(dp.begin(), dp.end(), a[i]) ;
int mx = it - dp.begin() + 1 ;
if (it == dp.end()) dp.push_back(a[i]) ;
else *it = a[i] ;
deque<int> dp1 ;
for (int j = n ; j > i ; j--) {
auto it1 = lower_bound(dp1.begin(), dp1.end(), a[j]) ;
int pos = it1 - dp1.begin() + 1 ;
if (it1 == dp1.begin()) dp1.push_front(a[j]) ;
else {
it1-- ;
*it1 = a[j] ;
}
int cur = it1 - dp1.begin() + 1 ;
if (a[j] - 1 >= l[i] && a[j] - 1 <= r[i]) {
// cout << mx + cur << "\n" ;
// cout << mx << ' ' << l[i] << ' ' << r[i] << ' ' << i << ' ' << dp1.size() << ' ' << j << "\n" ;
ret = max(ret, mx + cur) ;
}
}
}
cout << ret ;
return 0 ;
}
Compilation message (stderr)
glo.cpp: In function 'int32_t main()':
glo.cpp:45:17: warning: unused variable 'pos' [-Wunused-variable]
45 | int pos = it1 - dp1.begin() + 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... |