This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 300005
int par[MAX_N] ,beg[MAX_N];
int fnd(int u){
return par[u] == u? u : par[u] = fnd(par[u]);
}
void unn(int u ,int v){
u = fnd(u) ,v = fnd(v);
if(u == v)
return;
par[u] = v;
beg[v] = min(beg[v] ,beg[u]);
}
int sz ,tree[2*MAX_N];
void upd(int p ,int v){
for(tree[p+=sz]=v; p>1; p>>=1)
tree[p>>1] = max(tree[p] ,tree[p^1]);
}
int qry(int l ,int r){
int res = 0;
for (l+=sz ,r+=sz+1; l<r; l>>=1 ,r>>=1){
if(l&1) res = max(res ,tree[l++]);
if(r&1) res = max(res ,tree[--r]);
}
return res;
}
int main()
{
int n ,d;
scanf("%d%d",&n,&d) ,sz = n;
vector <int> a(n);
for(int&i : a)
scanf("%d",&i);
for(int i = 0; i < n; i++)
par[i] = beg[i] = i;
vector <int> ord(n);
iota(ord.begin() ,ord.end() ,0);
sort(ord.begin() ,ord.end() ,[&](int&i ,int&j){
return a[i] == a[j]? i > j : a[i] < a[j];
});
set <int> added;
for(int&i : ord){
auto it = added.insert(i).first;
if(it != added.begin() && i - *prev(it) <= d)
unn(*prev(it) ,i);
upd(i ,qry(beg[fnd(i)] ,i)+1);
if(next(it) != added.end() && *next(it) - i <= d)
unn(i ,*next(it));
}
printf("%d\n",qry(0 ,n-1));
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d%d",&n,&d) ,sz = n;
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d",&i);
| ~~~~~^~~~~~~~~
# | 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... |