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 ll long long
#define ld long double
//#define endl '\n'
const int N = 5e5 + 5;
long long mod = 998244353;
const int lim = 4e5 + 5;
const int lg = 19;
const int base = 311;
const long double eps = 1e-6;
ll st[N], dp[N], p[N], mn[N];
pair<ll, ll> a[N];
void upd(ll k, ll l, ll r, ll x, ll v){
if (x > r || x < l) return;
if (l == r){
st[k] = v;
return;
}
ll mid = (l + r) >> 1;
upd(k << 1, l, mid, x, v);
upd(k << 1 | 1, mid + 1, r, x, v);
st[k] = max(st[k << 1], st[k << 1 | 1]);
}
ll get(ll k, ll l, ll r, ll x, ll y){
if (x > r || y < l) return 0;
if (x <= l && y >= r) return st[k];
ll mid = (l + r) >> 1;
return max(get(k << 1, l, mid, x, y), get(k << 1 | 1, mid + 1, r, x, y));
}
ll find_set(ll u){
if (p[u] < 0) return u;
return p[u] = find_set(p[u]);
}
void dsu(ll u, ll v){
u = find_set(u);
v = find_set(v);
if (u == v) return;
if (p[u] > p[v]) swap(u, v);
p[u] += p[v];
p[v] = u;
mn[u] = min(mn[u], mn[v]);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("tests.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n, d;
cin >> n >> d;
for (int i = 1; i <= n; i++){
cin >> a[i].first;
a[i].second = i;
mn[i] = i;
p[i] = -1;
}
sort(a + 1, a + n + 1);
set<ll> s;
ll ans = 1;
for (int i = 1; i <= n; i++){
ll r = i;
while (r <= n && a[r].first == a[i].first){
auto it = s.lower_bound(a[r].second);
if (it != s.begin()){
it--;
if ((a[r].second - (*it)) <= d){
dp[a[r].second] = get(1, 1, n, mn[find_set(*it)], a[r].second) + 1;
} else {
dp[a[r].second] = 1;
}
} else {
dp[a[r].second] = 1;
}
ans = max(ans, dp[a[r].second]);
r++;
}
for (int j = i; j < r; j++){
upd(1, 1, n, a[j].second, dp[a[j].second]);
auto it = s.lower_bound(a[j].second);
if (it != s.begin()){
it--;
if ((a[j].second - (*it)) <= d){
dsu(*it, a[j].second);
}
it++;
}
if (it != s.end()){
if (((*it) - a[j].second) <= d){
dsu(*it, a[j].second);
}
}
s.insert(a[j].second);
}
i = r - 1;
}
/*for (int i = 1; i <= n; i++){
cout << dp[i] << " ";
}*/
cout << ans;
}
/*
Ans:
Out:
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("test.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... |