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 endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
vector<ll> a;
struct SegmentTree {
vector<ll> sgt;
SegmentTree(int n) {
sgt.resize(n, LLONG_MAX);
}
ll get(int k, int l, int r, ll v) {
if (l == r) return l;
int m = (l + r) / 2;
if (sgt[k * 2 + 1] < v) return get(k * 2 + 1, m + 1, r, v);
return get(k * 2, l, m, v);
}
void update(int k, int l, int r, int i, ll v) {
if (l > i || r < i) return;
if (l == r) {
sgt[k] = v;
return;
}
int m = (l + r) / 2;
update(k * 2, l, m, i, v);
update(k * 2 + 1, m + 1, r, i, v);
sgt[k] = min(sgt[k * 2], sgt[k * 2 + 1]);
}
} sgt(4 * (3e5 + 10));
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, d;
cin >> n >> d;
a.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
if (d == 1) {
deque<ll> dq;
int ans = 0;
dq.push_back(a[n - 1]);
for (int i = n - 2; i >= 0; i--) {
while (dq.size() && a[i] >= dq.front()) dq.pop_front();
dq.push_front(a[i]);
ans = max(ans, (int) dq.size());
}
cout << ans;
}
if (d == n) {
ll ans = 0;
multiset<ll> lis[n + 10];
lis[0].insert(0);
sgt.update(1, 0, n, 0, 0);
ll dp[n + 10];
dp[0] = 0;
for (int i = 1; i <= n; i++) {
ll mn;
if (i > d + 1) {
mn = *lis[dp[i - d - 1]].begin();
lis[dp[i - d - 1]].erase(lis[dp[i - d - 1]].find(a[i - d - 1]));
if (lis[dp[i - d - 1]].size()) {
if (*lis[dp[i - d - 1]].begin() != mn) sgt.update(1, 0, n, i - d - 1, *lis[dp[i - d - 1]].begin());
}
else sgt.update(1, 0, n, i - d - 1, LLONG_MAX);
}
dp[i] = sgt.get(1, 0, n, a[i]) + 1;
if (lis[dp[i]].size()) mn = *lis[dp[i]].begin();
lis[dp[i]].insert(a[i]);
if (lis[dp[i]].size() == 1 || mn != *lis[dp[i]].begin()) sgt.update(1, 0, n, dp[i], a[i]);
ans = max(ans, dp[i]);
// cout << i << " : " << dp[i] << endl;
}
cout << ans;
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:76:40: warning: 'mn' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | if (lis[dp[i]].size() == 1 || mn != *lis[dp[i]].begin()) sgt.update(1, 0, n, dp[i], a[i]);
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |