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")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
int st[4*MAXN];
void u(int l, int r, int tar, int idx, int val) {
if(l == r){
st[idx] = val;return;
}
int mid = (l+r) >> 1;
if(tar <= mid) u(l, mid, tar, 2*idx+1, val);
else u(mid+1, r, tar, 2*idx+2, val);
st[idx] = max(st[2*idx+1], st[2*idx+2]);
}
int qu(int l, int r, int constl, int constr, int idx) {
if(l<=constl && constr<=r) return st[idx];
int mid = (constl+constr) >> 1;
if(mid<l || r<constl) return qu(l, r, mid+1, constr, 2*idx+2);
if(constr<l || r<mid+1) return qu(l, r, constl, mid, 2*idx+1);
return max(qu(l, r, constl, mid, 2*idx+1), qu(l, r, mid+1, constr, 2*idx+2));
}
void update(int i, int v) {
u(0, MAXN-1, i, 0, v);
}
int query(int l, int r) {
return qu(l, r, 0, MAXN-1, 0);
}
void solve(int tc) {
int N,D;cin >> N >> D;
int A[N+1];
map<int, vector<int> > mp;
for(int i=1; i<=N; i++){
cin>>A[i];
mp[A[i]].push_back(i);
}
set<pair<int, int> > s;
s.insert({696969, 0});
s.insert({N, N});//actual .first: N+1 - .first
for(auto xz: mp) {
int x = xz.first;
map<int, int> pending;
for(int y: xz.second) {
pair<int, int> q = *s.lower_bound({N+1 - y, 0});
if(q.first == 696969) continue;
//cout << y << ": ";
//cout << "erase " << N+1 - q.first << " " << q.second << "\n";
if(N+1 - q.first + q.second <= y) continue;
s.erase(q);
q.first = N+1 - q.first;
pair<int, int> q1 = {q.first, y - q.first};
pair<int, int> q2 = {y + 1, q.first + q.second - 1 - y};
q1.first = N+1 - q1.first;
q2.first = N+1 - q2.first;
if(q1.second >= D && q1.second < q.second) {
s.insert(q1);
//cout << "insert " << N+1 - q1.first << " " << q1.second << "\n";
}
if(q2.second >= D && q2.second < q.second) {
s.insert(q2);
//cout << "insert " << N+1 - q2.first << " " << q2.second << "\n";
}
}
for(int y: xz.second) {
pair<int, int> q = *s.lower_bound({N+1 - (y - D), 0});
int lb = 1;
if(q.first != 696969) {
lb = N+1 - q.first;
}
//cout << y << ": " << lb << "\n";
int q0 = query(lb, y);
pending[y] = q0 + 1;
}
for(auto y: pending) {
update(y.first, y.second);
}
}
cout << query(1, N) << "\n";
//for(int i=1; i<=N; i++) cout << query(i, i) << " ";
//cout << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
Compilation message (stderr)
Main.cpp: In function 'void solve(long long int)':
Main.cpp:49:9: warning: unused variable 'x' [-Wunused-variable]
49 | int x = xz.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... |