//#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];
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;
if (p[i].F > p[i - 1].F){
mx[i] = mx[i - 1] + 1;
}
else{
mx[i] = 1;
}
}
sort (p + 1, p + n + 1);
ll mx = 0;
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){
upd (j, get (max (j - m, 1ll), j) + 1);
if (j >= n - m){
mx = max (mx, get (j, j));
}
}
v.clear();
}
v.pb (p[i].S);
}
sort (all (v));
reverse (all (v));
for (auto j: v){
upd (j, get (max (j - m, 1ll), j) + 1);
if (j >= n - m){
mx = max (mx, get (j, j));
}
}
cout << mx;
}
| # | 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... |