#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define int long long
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;
const long long INF = 1e18 + 5;
vector<int> tr(8*MAXN);
void update(int node, int l, int r, int pos, int val) {
if(l == r) {
tr[node] = val;
return;
}
int mid = (l+r)/2, lc = 2*node + 1, rc = 2*node + 2;
if(pos <= mid) update(lc, l, mid, pos, val);
if(pos > mid) update(rc, mid + 1, r, pos, val);
tr[node] = max(tr[lc],tr[rc]);
}
int query(int node, int l, int r, int ll, int rr) {
if(l > rr || r < ll) return 0;
if(l >= ll && r <= rr) return tr[node];
int mid = (l+r)/2, lc = 2*node + 1, rc = 2*node + 2;
return max(query(lc, l, mid, ll, rr),query(rc, mid + 1, r, ll, rr));
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, x;
cin >> n >> x;
vector<int> v(n+1), vx(n+1), aux;
for(int i = 1; i <= n; i++) {
cin >> v[i];
vx[i] = v[i] + x;
aux.push_back(v[i]);
aux.push_back(vx[i]);
}
//compressão de coordenadas
sort(all(aux));
aux.erase(unique(all(aux)), aux.end());
for(int i = 1; i <= n; i++) {
v[i] = lower_bound(all(aux), v[i]) - aux.begin() + 1;
vx[i] = lower_bound(all(aux), vx[i]) - aux.begin() + 1;
}
//valor da maior LIS acabando no i-ésimo cara
vector<int> sz(n+1);
sz[0] = 0;
for(int i = 1; i <= n; i++) {
int max_sz = query(0, 0, 2*n + 1, 1, v[i] - 1);
sz[i] = max_sz + 1;
update(0, 0, 2*n + 1, v[i], sz[i]);
}
//reiniciando a segment tree
for(int i = 0; i < 8*MAXN; i++) tr[i] = 0;
//calculando de trás pra frente
int ans = sz[n];
for(int i = n; i >= 1; i--) {
int max_sz = query(0, 0, 2*n + 1, vx[i] + 1, 2*n + 1);
update(0, 0, 2*n + 1, vx[i], max_sz + 1);
ans = max(ans, sz[i-1] + query(0, 0, 2*n + 1, v[i-1] + 1, 2*n + 1));
}
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... |