This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x
constexpr ll inf = 1e18, N = 1e6 + 10, mod = 1e9 + 7, pr = 1000696969;
int n, d, a[N], l[N], r[N], dp[N];
set<int> bad, to;
vector<pair<int, int> > vc;
struct node {
int val;
} x[N << 2];
void change(int l, int r, int ind, int val, int v) {
if(r - l < 2) {
x[v].val = val;
return;
}
int mid = (l + r) >> 1;
if(ind < mid) change(l, mid, ind, val, 2 * v);
else change(mid, r, ind, val, 2 * v + 1);
x[v].val = max(x[2 * v].val, x[2 * v + 1].val);
}
int get(int l, int r, int s, int e, int v) {
if(r <= s || l >= e) return 0;
if(l >= s && r <= e) return x[v].val;
int mid = (l + r) >> 1;
return max(get(l, mid, s, e, 2 * v), get(mid, r, s, e, 2 * v + 1));
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
cin >>n >>d;
for(int i = 0; i < n; i++) cin >>a[i], vc.push_back({a[i], i});
sort(All(vc), greater<pair<int, int> >());
for(auto i : vc) {
int ind = i.second, val = i.first;
auto it = to.lower_bound(ind);
if(it != to.end()) {
l[*it] = ind, r[ind] = *it;
}
if(it != to.begin()) {
it--;
l[ind] = *it, r[*it] = ind;
}
if(ind - l[ind] > d) bad.insert(ind);
dp[ind] = 1;
auto pos = bad.upper_bound(ind);
if(pos == bad.end()) {
// wtf(ind), wtf(n);
// cout<<endl;
dp[ind] += get(0, n, ind + 1, n, 1);
}
else {
pos--;
// wtf(ind), wtf(*pos + d);
// cout<<endl;
dp[ind] += get(0, n, ind + 1, *pos + d, 1);
}
change(0, n, ind, dp[ind], 1);
}
cout<<get(0, n, 0, n, 1);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:55:23: warning: unused variable 'val' [-Wunused-variable]
55 | int ind = i.second, val = i.first;
| ^~~
# | 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... |