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;
#define st first
#define nd second
#define pi pair<int, int>
#define pb push_back
constexpr int LIM = 300'000;
constexpr int base = (1 << 19);
int dp[LIM + 10];
pi tab[LIM + 10];
int ran[LIM + 10];
int tri[2 * base];
int tri2[2 * base];
void upd(int v, int x) {
v += base;
while(v > 0) {
tri[v] = min(tri[v], x);
v /= 2;
}
}
int que(int l, int r) {
l += base;
r += base;
int ans = 1e9;
while(l <= r) {
if(l & 1) {
ans = min(ans, tri[l]);
}
if(!(r & 1)) {
ans = min(ans, tri[r]);
}
l = (l + 1) / 2;
r = (r - 1) / 2;
}
return ans;
}
void upd2(int l, int r, int x) {
l += base;
r += base;
while(l <= r) {
if(l & 1) {
tri2[l] = max(tri2[l], x);
}
if(!(r & 1)) {
tri2[r] = max(tri2[r], x);
}
l = (l + 1) / 2;
r = (r - 1) / 2;
}
}
int que2(int v) {
v += base;
int ans = 0;
while(v > 0) {
ans = max(ans, tri2[v]);
v /= 2;
}
return ans;
}
int main() {
int n, d;
cin >> n >> d;
for(int i = 0; i < 2 * base; i++) {
tri[i] = 1e9;
}
for(int i = 1; i <= n; i++) {
cin >> tab[i].st;
tab[i].nd = -i;
upd(i, tab[i].st);
}
int ost = tab[n].st;
vector<pi>prz;
for(int i = n - d; i >= 1; i--) {
if(!prz.size() || prz[0].nd <= tab[i].st) {
ran[i] = n;
}
else {
int l = 0;
int r = prz.size() - 1;
while(l < r) {
int mid = (l + r + 1) / 2;
if(prz[mid].nd <= tab[i].st) {
r = mid - 1;
}
else {
l = mid;
}
}
ran[i] = prz[l].st + d - 1;
}
int mn = que(i, i + d - 1);
while(prz.size() && prz.back().nd <= mn) {
prz.pop_back();
}
prz.pb({i, mn});
}
for(int i = n; i >= n - d + 1; i--) {
ran[i] = n;
}
sort(tab + 1, tab + n + 1);
int ans = 0;
for(int i = 1; i <= n; i++) {
tab[i].nd *= -1;
dp[tab[i].nd] = que2(tab[i].nd) + 1;
upd2(tab[i].nd, ran[tab[i].nd], dp[tab[i].nd]);
if(tab[i].st >= ost && ran[tab[i].nd] == n) {
ans = max(ans, dp[tab[i].nd]);
}
}
cout << ans << '\n';
}
# | 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... |