#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
struct ST{
vector<int> t;
int n;
ST(int ns){
n = ns;
t.resize(4 * n);
}
void upd(int v, int tl, int tr, int& p, int& x){
if (tl == tr){
t[v] = max(t[v], x);
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
if (p <= tm){
upd(vv, tl, tm, p, x);
}
else {
upd(vv + 1, tm + 1, tr, p, x);
}
t[v] = max(t[vv], t[vv + 1]);
}
void upd(int p, int x){
upd(1, 1, n, p, x);
}
int get(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return 0;
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2, vv = 2 * v;
return max(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
}
int get(int l, int r){
if (l > r) return 0;
return get(1, 1, n, l, r);
}
void clear(){
fill(t.begin(), t.end(), 0);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, X; cin>>n>>X;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++){
cin>>a[i];
}
auto lis = [&](vector<int> x){
vector<pii> all;
for (int i = 1; i <= n; i++){
all.pb({x[i], i});
}
sort(all.begin(), all.end());
int i = 0, cc = 1;
while (i < n){
int j = i;
while (j < n && all[i].ff == all[j].ff){
x[all[j].ss] = cc;
j++;
}
i = j; cc++;
}
ST T(n);
vector<int> dp(n + 1);
int ret = 0;
for (int i = 1; i <= n; i++){
dp[i] = 1 + T.get(1, x[i] - 1);
ret = max(ret, dp[i]);
T.upd(x[i], dp[i]);
}
return ret;
};
// LIS(a1, a2, ..., ai, a[i + 1] + X, ..., an + X)
int out = lis(a);
for (int i = 1; i <= n; i++){
a[i] -= X;
out = max(out, lis(a));
}
for (int i = 1; i <= n; i++) a[i] += X;
vector<int> all;
for (int i = 1; i <= n; i++){
all.pb(a[i]);
}
sort(all.begin(), all.end());
vector<int> vv = {0};
int i = 0;
while (i < n){
int j = i;
while (j < n && all[i] == all[j]){
j++;
}
vv.pb(all[i]);
i = j;
}
vector<int> :: iterator it;
vector<int> D2(n + 1);
ST T(n);
for (int i = n; i > 0; i--){
it = lower_bound(vv.begin(), vv.end(), a[i]);
int j = (int) (it - vv.begin());
D2[i] = 1 + T.get(j + 1, n);
T.upd(j, D2[i]);
}
vector<int> dp2(n + 1);
T.clear();
for (int i = 1; i <= n; i++){
it = lower_bound(vv.begin(), vv.end(), a[i] + X);
int j = (int) (it - vv.begin());
out = max(out, T.get(1, j - 1) + D2[i]);
it = lower_bound(vv.begin(), vv.end(), a[i]);
j = (int) (it - vv.begin());
dp2[i] = 1 + T.get(1, j - 1);
T.upd(j, dp2[i]);
}
cout<<out<<"\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... |