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 ;
struct SegTree {
int _n ;
struct Node {
ll value = 0 ;
Node() {}
Node(ll x) {
value = x ;
}
};
Node combine(Node l, Node r) {
if (l.value > r.value) return l ;
return r ;
}
vector<Node> t ;
SegTree(int len) {
_n = len ;
t.assign(len * 4 + 1, Node(0)) ;
}
void modify(int i, ll x, int v, int tl, int tr) {
if (tl == tr) {
t[v] = combine(x, t[v]) ;
} else {
int tm = (tl + tr) >> 1 ;
if (i <= tm)
modify(i, x, v << 1, tl, tm) ;
else
modify(i, x, v << 1 | 1, tm + 1, tr) ;
t[v] = combine(t[v * 2], t[v * 2 + 1]) ;
}
}
void modify(int i, ll x) {
modify(i, x, 1, 1, _n) ;
}
Node get(int l, int r, int v, int tl, int tr) {
if (l > tr || r < tl) return Node(0) ;
if (l <= tl && tr <= r) return t[v] ;
int tm = (tl + tr) >> 1 ;
Node L = get(l, r, v << 1, tl, tm) ;
Node R = get(l ,r, v << 1 | 1, tm + 1, tr) ;
return combine(L, R) ;
}
Node get(int l, int r) {
if (l > r) return Node(0) ;
return get(l, r, 1, 1, _n) ;
}
};
int32_t main() {
ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
int n , x ;
cin >> n >> x ;
vector<int> a(n + 1), c ;
for (int i = 1 ; i <= n ; i++) {
cin >> a[i] ;
c.push_back(a[i]) ;
c.push_back(a[i] - 1) ;
}
sort(c.begin(), c.end()) ;
c.erase(unique(c.begin(), c.end()), c.end()) ;
auto get = [&](int i) -> int {
return (lower_bound(c.begin(), c.end(), i) - c.begin() + 1) ;
};
int m = c.size() ;
SegTree s(m) ;
vector<int> pre(n + 1, 1) ;
int ret = 0 ;
for (int i = 1 ; i <= n ; i++) {
int x = get(a[i]), downx = get(a[i] - 1) ;
pre[i] = max(pre[i] * 1ll, s.get(1, downx).value + 1) ;
s.modify(x, pre[i]) ;
SegTree t(m) ;
for (int j = n ; j > i ; j--) {
int y = get(a[j]), upy = get(a[j] + 1) ;
int cur = max(1ll, t.get(upy, m).value + 1) ;
t.modify(y, cur) ;
if(a[j] - 1 >= a[i] - x && a[j] + 1 <= a[i] + x) {
ret = max(ret, pre[i] + cur) ;
}
}
ret = max(ret, pre[i]) ;
}
cout << ret ;
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... |