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>
#define MAXN 300010
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define INF 1000000000
#define mid (l+r)/2
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
using namespace std;
int R[MAXN], A[MAXN], seg[4*MAXN], dp[MAXN];
vector<pii> v;
set<int> s;
void update(int node, int l, int r, int idx, int val) {
if(l == r) {
seg[node] = val;
return;
}
if(idx <= mid) update(2*node, l, mid, idx, val);
else update(2*node+1, mid+1, r, idx, val);
seg[node] = max(seg[2*node], seg[2*node+1]);
}
int query(int node, int l, int r, int L, int R) {
if(L <= l && r <= R) return seg[node];
int rez = 0;
if(L <= mid) rez = max(rez, query(2*node, l, mid, L, R));
if(R > mid) rez = max(rez, query(2*node+1, mid+1, r, L, R));
return rez;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int N, D; cin >> N >> D;
for(int i = 1; i <= N; i++) {
cin >> A[i];
v.pb({A[i], -i});
if(i <= N-D+1) s.insert(i);
}
sort(v.begin(), v.end());
for(auto xx : v) {
int idx = -xx.se;
auto x = s.upper_bound(idx);
R[idx] = min(N, (x == s.end() ? N:*x+D-1));
while(!s.empty()) {
auto x = s.lower_bound(max(0, idx-D+1));
if(x == s.end() || *x > idx) break;
s.erase(x);
}
}
fill(seg, seg+4*MAXN, -INF);
update(1, 1, N, N, 0);
sort(v.rbegin(), v.rend());
for(int i = 0; i < N; i++) {
int idx = -v[i].se;
dp[idx] = query(1, 1, N, idx, R[idx])+1;
update(1, 1, N, idx, dp[idx]);
}
int rez = 0;
for(int i = 1; i <= N; i++) {
rez = max(rez, dp[i]);
}
cout << rez;
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... |