#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
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, d; cin>>n>>d;
vector<int> a(n + 1);
vector<pii> all;
for (int i = 1; i <= n; i++){
cin>>a[i];
all.pb({a[i], i});
}
sort(all.begin(), all.end());
int i = 0, m = 0;
while (i < n){
int j = i; m++;
while (j < n && all[i].ff == all[j].ff){
a[all[j].ss] = m;
j++;
}
i = j;
}
set<pii> t1, t2, x1, x2;
t1.insert({0, 0}); x1.insert({0, 0});
t2.insert({0, 0}); x2.insert({0, 0});
auto T = [&](int f){
auto it = t1.lower_bound({f + 1, 0}); it--;
return (*it).ss;
};
auto X = [&](int f){
auto it = x1.lower_bound({f + 1, 0}); it--;
return (*it).ss;
};
for (int i = 1; i <= n; i++){
int v = (T(a[i] - 1) >= (i - d)) ? (X(a[i] - 1) + 1) : 1;
if (T(a[i]) >= (i - d)) v = max(v, X(a[i]));
int j = a[i] + 1;
auto it = t2.lower_bound({i - d, 0});
if (it == t2.end()){
j = m + 1;
}
else {
j = max(j, (*it).ss);
}
it = x2.lower_bound({v, 0});
if (it == x2.end()){
j = m + 1;
}
else {
j = max(j, (*it).ss);
}
if (a[i] < j){
while (true){
it = x1.lower_bound({a[i], 0});
if (it == x1.end() || (*it).ff >= j) break;
x2.erase({(*it).ss, (*it).ff});
x1.erase(it);
}
x1.insert({a[i], v});
x2.insert({v, a[i]});
}
while (true){
it = t1.lower_bound({a[i], 0});
if (it == t1.end()) break;
t2.erase({(*it).ss, (*it).ff});
t1.erase(it);
}
t1.insert({a[i], i});
t2.insert({i, a[i]});
}
cout<<(*prev(x1.end())).ss<<"\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... |