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;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 3e5 + 100;
int a[N];
bool cmp(int i, int j) {
if (a[i] != a[j])
return a[i] < a[j];
return j < i;
}
int mn[N], p[N];
int get(int a) {
return p[a] ? get(p[a]) : a;
}
int sz[N], seg[N * 4];
void unite(int a, int b) {
a = get(a), b = get(b);
if (a == b)
return;
if (sz[a] < sz[b])
swap(a, b);
sz[a] += sz[b];
p[b] = a;
mn[a] = min(mn[a], mn[b]);
}
void upd(int id, int l, int r, int p, int v) {
if (l > p || r < p)
return;
if (l == r) {
seg[id] = max(seg[id], v);
return;
}
int m = (l + r) / 2;
upd(id * 2, l, m, p, v);
upd(id * 2 + 1, m + 1, r, p, v);
seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u || u > v)
return 0;
if (l >= u && r <= v)
return seg[id];
int m = (l + r) / 2;
return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
ll solve() {
int n, d;
cin >> n >> d;
for (int i = 1; i <= n; i++) {
sz[i] = 1;
mn[i] = i;
}
vector <int> v;
for (int i = 1; i <= n; i++) {
cin >> a[i];
v.pb(i);
}
sort(all(v), cmp);
set <int> st;
int ans = 1;
for (auto x : v) {
auto it = st.lower_bound(x);
if (it != st.end()) {
if (abs(*it - x) <= d) {
unite(*it, x);
}
}
if (it != st.begin()) {
auto t = prev(it);
if (abs(*t - x) <= d) {
unite(*t, x);
}
}
int v = mn[get(x)];
int cur = get(1, 1, n, v, x - 1) + 1;
ans = max(ans, cur);
upd(1, 1, n, x, cur);
st.insert(x);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
//solve();
cout << solve() << '\n';
}
return 0;
}
# | 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... |