//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int N = 500001;
using namespace std;
ll n, m, t[N * 4],mx[N],used[N];
pair <ll, ll> p[N];
vector <ll> v;
void upd (ll idx, ll val, ll tl = 1, ll tr = n, ll v = 1){
if (tl == tr){
t[v] = val;
return;
}
ll tm = (tl + tr) / 2;
if (tm < idx){
upd (idx, val, tm + 1, tr, v + v + 1);
}
else{
upd (idx, val, tl, tm, v + v);
}
t[v] = max (t[v + v], t[v + v + 1]);
}
ll get (ll l, ll r, ll tl = 1, ll tr = n, ll v = 1){
if (tr < l || tl > r){
return 0;
}
if (l <= tl && tr <= r){
return t[v];
}
ll tm = (tl + tr) / 2;
return max (get (l, r, tl, tm, v + v), get (l, r, tm + 1, tr, v + v + 1));
}
signed main (){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++){
cin >> p[i].F;
p[i].S = i;
}
sort (p + 1, p + n + 1);
for (int i = 1; i <= n; i++){
if (p[i].F != p[i - 1].F){
sort (all (v));
reverse (all (v));
for (auto j: v){
ll L = max (j - m, 1ll);
for (int k = j; k >= 1; k--){
if (used[k]){
if (L > k + 1){
L = k + 1;
}
break;
}
}
upd (j, get (L, j) + 1);
used[j] = 1;
}
v.clear();
}
v.pb (p[i].S);
}
sort (all (v));
reverse (all (v));
for (auto j: v){
ll L = max (j - m, 1ll);
for (int k = j; k >= 1; k--){
if (used[k]){
if (L > k + 1){
L = k + 1;
}
break;
}
}
upd (j, get (L, j) + 1);
used[j] = 1;
}
cout << t[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... |