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>
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())
using namespace std;
const int N = 1e6+7;
const int M = 1e9+7;
const ll oo = 3e18;
int n, a[N], d, dp[N];
ii b[N];
int st[N];
int p[N], mi[N];
int Find(int u) {
if (u == p[u]) return u;
p[u] = Find(p[u]);
return p[u];
}
void Join(int u, int v) {
u = Find(u);
v = Find(v);
if (u == v) return;
if (v < u) swap(u, v);
p[v] = u;
mi[u] = mi[v] = min(mi[u], mi[v]);
}
void up(int id, int l, int r, int u, int v) {
if (l > u or r < u) return;
if (l == r) {
st[id] = v;
return;
}
int mid = l + r >> 1;
up(id * 2, l, mid, u, v);
up(id * 2 + 1, mid + 1, r, u, v);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v) {
if (u > v) return 0;
if (l > v or r < u) return 0;
if (u <= l and r <= v) return st[id];
int mid = l + r >> 1;
return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#define TASK ""
if (fopen (".inp", "r")) {
freopen (".inp", "r", stdin);
freopen (".out", "w", stdout);
}
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> d;
vector<int> v;
For (i, 1, n) cin >> a[i], b[i] = {a[i], i};
For (i, 1, n) dp[i] = 0;
sort (b + 1, b + n + 1, [](ii a, ii b) {
if (a.fi == b.fi) return a.se > b.se;
return a.fi < b.fi;
});
memset(st, 0, sizeof st);
int ans = 0;
For (i, 1, n) p[i] = i, mi[i] = i;
set<int> s;
For (i, 1, n) {
auto it = s.lower_bound(b[i].se);
if (it != s.end()) if (*it - b[i].se <= d) Join(*it, b[i].se);
if (it != s.begin()) {it--; if (b[i].se - *it <= d) Join(*it, b[i].se);}
s.insert(i);
dp[b[i].se] = max(dp[b[i].se], get(1, 1, n, mi[Find(b[i].se)], i - 1)) + 1;
ans = max(ans, dp[b[i].se]);
up(1, 1, n, b[i].se, dp[b[i].se]);
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void up(int, int, int, int, int)':
Main.cpp:43:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int get(int, int, int, int, int)':
Main.cpp:53:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int main()':
Main.cpp:62:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen (".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:63:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen (".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | 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... |