This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//problem: https://oj.uz/problem/view/JOI21_financial
//solution: https://codeforces.com/blog/entry/91295
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define rep1(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define all(x) x.begin(), x.end()
struct segtree {
int sz = 1;
vector<int> maxi;
void init(int SIZE) {
while (sz < SIZE) sz *= 2;
maxi.resize(2 * sz);
}
void update(int i, int v, int x, int lx, int rx) {
if (rx - lx == 1) {
maxi[x] = v;
return;
}
int m = (lx + rx) >> 1;
if (i < m) update(i, v, 2 * x + 1, lx, m);
else update(i, v, 2 * x + 2, m, rx);
maxi[x] = max(maxi[2 * x + 1], maxi[2 * x + 2]);
}
void update(int i, int v) {update(i, v, 0, 0, sz);}
int query(int l, int r, int x, int lx, int rx) {
if (rx <= l || r <= lx) return 0;
if (l <= lx && rx <= r) return maxi[x];
int m = (lx + rx) >> 1;
return max(query(l, r, 2 * x + 1, lx, m), query(l, r, 2 * x + 2, m, rx));
}
int query(int l, int r) {return query(l, r, 0, 0, sz);}
};
bool cmp(pii x, pii y) {
return make_pair(x.ff, -x.ss) < make_pair(y.ff, -y.ss);
}
const int N = 3e5 + 5;
int pa[N];
int fp(int u) {
if (u == pa[u]) return u;
return pa[u] = fp(pa[u]);
}
void join(int x, int y) {
x = fp(x), y = fp(y);
if (x == y) return;
if (x < y) swap(x, y);
pa[x] = y;
return;
}
void solve() {
int n, d; cin >> n >> d;
rep(i, 1, n) pa[i] = i;
vector<pii> a(n + 1);
rep(i, 1, n) cin >> a[i].ff, a[i].ss = i;
sort(a.begin() + 1, a.end(), cmp);
segtree st;
st.init(n + 1);
st.update(0, 5);
set<int> pos;
int ans = 0;
rep(i, 1, n) {
auto it = pos.lower_bound(a[i].ss);
if (it != pos.end()) {
int id = *it;
if (abs(id - a[i].ss) <= d) join(a[i].ss, id);
}
if (it != pos.begin()) {
--it;
int id = *it;
if (abs(id - a[i].ss) <= d) join(a[i].ss, id);
}
int left = fp(a[i].ss);
int tmp = st.query(left, a[i].ss) + 1;
st.update(a[i].ss, tmp);
ans = max(ans, tmp);
pos.insert(a[i].ss);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
solve();
}
# | 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... |