This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
using namespace std;
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
vii v;
int n, d, a[mxN];
int segtree[4 * mxN], lazy[4 * mxN];
void down(int idx) {
int val = lazy[idx];
segtree[idx << 1] = max(segtree[idx << 1], val);
lazy[idx << 1] = max(lazy[idx << 1], val);
segtree[idx << 1 | 1] = max(segtree[idx << 1 | 1], val);
lazy[idx << 1 | 1] = max(lazy[idx << 1 | 1], val);
lazy[idx] = 0;
}
void update(int l, int r, int idx, int u, int v, int val) {
if(r < u || v < l || r < l) return;
if(u <= l && r <= v) {
segtree[idx] = max(segtree[idx], val);
lazy[idx] = max(lazy[idx], val);
return;
}
down(idx);
int mid = (l + r) >> 1;
update(l, mid, idx << 1, u, v, val);
update(mid + 1, r, idx << 1 | 1, u, v, val);
segtree[idx] = max(segtree[idx << 1], segtree[idx << 1 | 1]);
}
int get(int l, int r, int idx, int pos) {
if(l == r) {
return segtree[idx];
}
down(idx);
int mid = (l + r) >> 1;
if(pos <= mid) return get(l, mid, idx << 1, pos);
return get(mid + 1, r, idx << 1 | 1, pos);
}
void solve() {
cin >> n >> d;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
v.emplace_back(a[i], i);
}
sort(all(v), [&](pii x, pii y) {
if(x.ff == y.ff) return (x.ss > y.ss);
return (x.ff < y.ff);
});
for(pii &tmp : v) {
int cur = get(1, n, 1, tmp.ss) + 1;
update(1, n, 1, tmp.ss, min(n, tmp.ss + d), cur);
}
cout << get(1, n, 1, n) << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# | 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... |