#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define pb push_back
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int M = 1e6 + 10;
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
int n, d, a[M];
ii b[M];
int pre[M], f[M];
int t[4 * M];
void upd(int s,int l,int r,int pos,int val)
{
if(l > pos || pos > r) return ;
if(l == r) {
t[s] = val;
return ;
}
int mid = (l + r)/2;
upd(s * 2, l, mid, pos, val);
upd(s * 2 + 1, mid + 1, r, pos, val);
t[s] = max(t[s * 2], t[s * 2 + 1]);
}
int get(int s,int l,int r,int u,int v)
{
if(l > v || r < u) return 0;
if(u <= l && r <= v) return t[s];
int mid = (l + r)/2;
return max(get(s * 2, l, mid, u, v), get(s * 2 + 1, mid + 1, r, u, v));
}
bool dd[M];
int id[M];
set<int> s;
int finded(int u) {
return id[u] < 0 ? u : id[u] = finded(id[u]);
}
void unioned(int u,int v)
{
u = finded(u);
v = finded(v);
if(u == v) return ;
id[v] += id[u];
id[u] = v;
if(abs(id[v]) >= d) s.insert(v + 1);
return ;
}
void presolve()
{
// if(d == 1) {
// for(int i = 1;i <= n; i++) {
// if(a[i - 1] <= a[i]) pre[i] = i - 1;
// else pre[i] = i;
// }
// return ;
// }
for(int i = 1;i <= n; i++) id[i] = -1;
for(int j = n; j != 0; j--) {
int i = b[j].second;
dd[i] = true;
if(s.size() != 0) {
auto idx = s.upper_bound(i);
if(idx != s.begin()) {
idx = prev(idx);
pre[i] = *idx;
}
}
if(d == 1) s.insert(i) ;
else {
if(dd[i - 1]) unioned(i - 1, i);
if(dd[i + 1]) unioned(i, i + 1);
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen("1.inp","r")) {
freopen("1.inp","r",stdin);
freopen("1.out","w",stdout);
}
#define task ""
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> d;
for(int i = 1;i <= n; i++) cin >> a[i];
for(int i = 1;i <= n; i++) {
b[i].first = a[i];
b[i].second = i;
}
set<int> s;
for(int i = d + 1;i <= n; i++) s.insert(i);
sort(b + 1, b + n + 1);
presolve();
int e = 0;
for(int j = 1;j <= n; j++) {
while(j != e + 1 && b[e + 1].first != b[j].first) {
e++;
int idx = b[e].second;
upd(1, 1, n, idx, f[idx]);
}
int i = b[j].second;
f[i] = get(1, 1, n, pre[i], i) + 1;
// cout << i << " " << pre[i] << " " << f[i] << "\n";
}
int kq = 0;
for(int i = 1;i <= n; i++) kq = max(kq,f[i]);
cout << kq;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:95:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen("1.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:96:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("1.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:101:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:102:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |