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>
//#pragma GCC optimize("O2")
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
int n, d;
const int N = 3e5 + 5;
const int L = (1 << 19);
int a[N];
int mini[N];
int reach[N];
int seg[(L << 1)];
void upd(int b, int e, int x) {
e++;
b += L;
e += L;
while (b != e) {
if (b & 1) {
seg[b] = max(seg[b], x);
b++;
}
if (e & 1) {
e--;
seg[e] = max(seg[e], x);
}
b >>= 1;
e >>= 1;
}
}
int get(int ind) {
ind += L;
int ans = 0;
while (ind) {
ans = max(ans, seg[ind]);
ind >>= 1;
}
return ans;
}
void input() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> a[i];
//a[i] = i;
}
}
void cremini() {
deque<int> dq;
for (int i = 1; i < d; i++) {
while (!dq.empty()) {
if (a[dq.front()] < a[i]) {
break;
}
dq.pop_front();
}
dq.push_front(i);
}
for (int i = d; i <= n; i++) {
while (!dq.empty()) {
if (a[dq.front()] < a[i]) {
break;
}
dq.pop_front();
}
dq.push_front(i);
if (dq.back() == i - d) {
dq.pop_back();
}
mini[i] = a[dq.back()];
}
}
void crereach() {
vector<int> obs;
for (int i = n; i >= 1; i--) {
int lobs = sz(obs), robs = -1;
while (lobs - 1 != robs) {
int m = (lobs + robs + 1) / 2;
if (mini[obs[m]] > a[i]) {
robs = m;
}
else {
lobs = m;
}
}
reach[i] = n;
if (robs != -1) {
reach[i] = obs[robs];
}
if (i + d - 1 <= n) {
while (!obs.empty()) {
if (mini[i + d - 1] < mini[obs.back()]) {
break;
}
obs.pop_back();
}
obs.pb(i + d - 1);
}
}
}
void solve() {
cremini();
crereach();
int dp[n + 1];
pair<int, int> nums[n + 1];;
for (int i = 1; i <= n; i++) {
nums[i] = mp(a[i], -i);
}
sort(nums + 1, nums + n + 1);
for (int i = 1; i <= n; i++) {
int ind = -nums[i].se;
dp[ind] = get(ind) + 1;
upd(ind + 1, reach[ind], dp[ind]);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
input();
solve();
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... |