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;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 3e5 + 3;
struct ST{
int n;
vector<int> st;
void init(int _n){
n = _n;
st.resize(4 * n + 6, -1e9);
}
void _update(int id, int l, int r, int pos, int val){
if(l == r){
st[id] = max(st[id], val);
return;
}
int mid = (l + r) >> 1;
if(pos <= mid)
_update(id << 1, l, mid, pos, val);
else
_update(id << 1 | 1, mid + 1, r, pos, val);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int _get(int id, int l, int r, int tl, int tr){
if(l > tr or tl > r)
return -1e9;
if(tl <= l and r <= tr)
return st[id];
int mid = (l + r) >> 1;
return max(_get(id << 1, l, mid, tl, tr), _get(id << 1 | 1, mid + 1, r, tl, tr));
}
void update(int pos, int val){ _update(1, 1, n, pos, val); }
int get(int l, int r){ return _get(1, 1, n, l, r); }
};
int n, d, a[N];
pair<int, int> b[N];
int p[N], mn[N];
ST st;
int find_set(int u){ return p[u] < 0 ? u : p[u] = find_set(p[u]); }
void join_set(int u, int v){
u = find_set(u), v = find_set(v);
if(u == v)
return;
if(p[u] > p[v])
swap(u, v);
mn[u] = min(mn[u], mn[v]);
p[u] += p[v];
p[v] = u;
}
void solve(){
cin >> n >> d;
for(int i = 1; i <= n; ++i){
cin >> a[i];
b[i] = {a[i], -i};
}
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; ++i)
p[i] = -1, mn[i] = i;
st.init(n);
set<int> pos;
for(int i = 1; i <= n; ++i){
int id = -b[i].S;
auto it = pos.lower_bound(id);
if(it != pos.end() and *it - id <= d)
join_set(id, *it);
if(it != pos.begin() and id - *(--it) <= d)
join_set(*it, id);
pos.insert(id);
int f = max(0, st.get(mn[find_set(id)], id - 1)) + 1;
st.update(id, f);
}
cout << st.get(1, n);
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | 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... |