#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define SZ(x) ((int) (x).size())
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define debug(x) cout << "[" << #x << " = " << (x) << "]" << endl
#define left __left
#define right __right
#define prev __prev
#define fi first
#define se second
template <class X, class Y>
bool maximize(X &x, Y y) {
if (x < y) return x = y, true;
else return false;
}
template <class X, class Y>
bool minimize(X &x, Y y) {
if (x > y) return x = y, true;
else return false;
}
template <class T>
void remove_dup(vector <T> &v) {
sort(ALL(v));
v.resize(unique(ALL(v)) - v.begin());
}
int POS(int x, const vector <int> &v) {
return lower_bound(ALL(v), x) - v.begin() + 1;
}
int n, maxRange;
#define MAX_N 300'300
int a[MAX_N + 7];
int dp[MAX_N + 7], maxPos[MAX_N + 2];
struct maxTree {
int seg[4 * MAX_N + 7];
void update(int id, int l, int r, int pos, int val) {
if (l > pos || pos > r) return;
if (l == r) {
maximize(seg[id], val);
return;
}
int g = (l + r) >> 1;
if (pos <= g) update(id << 1, l, g, pos, val);
else update(id << 1 | 1, g + 1, r, pos, val);
seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || u > r) return 0;
if (u <= l && r <= v) return seg[id];
int g = (l + r) >> 1;
return max(get(id << 1, l, g, u, v), get(id << 1 | 1, g + 1, r, u, v));
}
void update(int pos, int val) {
update(1, 1, n, pos, val);
}
int get(int l, int r) {
if (l > r) return 0;
return get(1, 1, n, l, r);
}
} st;
namespace subtask1 {
bool check() {
return (n <= 20);
}
int ids[25], cnt_ids = 0;
void solve() {
int ans = 0;
FOR(mask, 1, MASK(n) - 1) {
bool ok = true;
cnt_ids = 0;
FOR(i, 0, n - 1) if (BIT(mask, i)) {
if (cnt_ids > 0) {
if (i + 1 - ids[cnt_ids] > maxRange) {ok = false; break;}
}
ids[++cnt_ids] = i + 1;
}
if (!ok) continue;
int res = 1, ma = a[ids[1]];
FOR(i, 2, cnt_ids) {
if (a[ids[i]] > ma) {
++res;
ma = a[ids[i]];
}
}
maximize(ans, res);
}
cout << ans;
}
};
namespace subtask3 {
bool check() {
return (n <= 7000);
}
void solve() {
/// dp[i]: day con dai nhat ma co a_i la max;
FOR(i, 1, n) dp[i] = 1;
FOR(i, 1, n) {
int lstPos = i;
FOR(j, i + 1, n) {
if (a[j] <= a[i]) {
if (j - lstPos <= maxRange) lstPos = j;
else break;
}
}
FOR(j, i + 1, min(n, lstPos + maxRange)) if (a[j] > a[i]) maximize(dp[j], dp[i] + 1);
}
cout << *max_element(dp + 1, dp + 1 + n);
}
};
namespace subtask4 {
bool check() {
return (maxRange == 1);
}
int dp[MAX_N + 2];
void solve() {
int ans = 0;
stack <int> st;
dp[n] = 1;
st.push(n);
FORD(i, n - 1, 1) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
dp[i] = 1 + (!st.empty() ? dp[st.top()] : 0);
st.push(i);
}
cout << *max_element(dp + 1, dp + 1 + n);
}
};
namespace subtask5 {
bool check() {
return (maxRange == n);
}
void solve() {
vector <int> v;
FOR(i, 1, n) {
if (v.empty()) v.push_back(a[i]);
else {
if (v.back() < a[i]) v.push_back(a[i]);
else {
int id = lower_bound(ALL(v), a[i]) - v.begin();
v[id] = a[i];
}
}
}
cout << SZ(v);
}
};
namespace subtask6 {
int perm[MAX_N + 2];
bool cmp(int x, int y) {
if (a[x] != a[y]) return a[x] > a[y];
return (x < y);
}
void solve() {
/// dp[i]: day con dai nhat ma co a_i la max;
FOR(i, 1, n) dp[i] = 1;
FOR(i, 1, n) {
int lstPos = i;
FOR(j, i + 1, n) {
if (a[j] <= a[i]) {
if (j - lstPos <= maxRange) lstPos = j;
else break;
}
}
maxPos[i] = min(n, lstPos + maxRange);
}
FOR(i, 1, n) perm[i] = i;
sort(perm + 1, perm + 1 + n, cmp);
FOR(i, 1, n) dp[i] = 1;
FOR(i, 1, n) {
maximize(dp[perm[i]], st.get(perm[i], maxPos[perm[i]]) + 1);
st.update(perm[i], dp[perm[i]]);
}
cout << *max_element(dp + 1, dp + 1 + n);
}
};
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
// if (fopen("financial.inp","r")) {
// freopen("financial.inp","r",stdin);
// freopen("financial.out","w",stdout);
// }
cin >> n >> maxRange;
vector <int> v;
FOR(i, 1, n) cin >> a[i], v.push_back(a[i]);
remove_dup(v);
FOR(i, 1, n) a[i] = POS(a[i], v);
// if (subtask4 :: check()) return subtask4 :: solve(), 0;
// if (subtask5 :: check()) return subtask5 :: solve(), 0;
// if (subtask1 :: check()) return subtask1 :: solve(), 0;
subtask3 :: solve();
return 0;
}
/* Discipline - Calm */
# | 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... |