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 ll long long
using namespace std;
const int MAX = 3e5 + 3;
const ll mod = 1e9 + 7;
int n, D;
int maxVal[MAX << 1];
void modify(int i, int val) {
i += n;
maxVal[i] = val;
for (; i > 1; i >>= 1) maxVal[i >> 1] = max(maxVal[i], maxVal[i ^ 1]);
}
int get(int l, int r) {
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, maxVal[l++]);
if (r & 1) res = max(res, maxVal[--r]);
}
return res;
}
void Solve() {
cin >> n >> D;
vector <int> a(n);
vector <int> id(n);
vector <int> pa(n);
vector <int> mn(n);
vector <int> dp(n);
for (int &x : a) cin >> x;
iota(pa.begin(), pa.end(), 0);
iota(mn.begin(), mn.end(), 0);
iota(id.rbegin(), id.rend(), 0);
sort(id.begin(), id.end(), [&](const int &i, const int &j) {
return a[i] < a[j];
});
function <int(int)> findpa = [&](int i) -> int {
return i == pa[i] ? i : pa[i] = findpa(pa[i]);
};
auto join = [&](int u, int v) -> void {
u = findpa(u);
v = findpa(v);
if (u == v) return;
pa[v] = u;
mn[u] = min(mn[u], mn[v]);
};
set <int> S;
for (int &i : id) {
auto p = S.lower_bound(i);
if (p != S.end() && *p - i <= D) join(*p, i);
if (p != S.begin() && i - *(prev(p)) <= D) join(i, *prev(p));
S.insert(i);
dp[i] = get(mn[findpa(i)], i) + 1;
modify(i, dp[i]);
}
cout << *max_element(dp.begin(), dp.end());
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define TASK "TASK"
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
Solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:75:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:76:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | 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... |