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>
// #define int long long
#define fi first
#define se second
using namespace std;
const int inf = 1e9;
struct node {
int val;
int st, en;
node *left, *right;
void build(int start, int end) {
st = start;
en = end;
val = 0;
}
void lazy() {
if (left == NULL) {
int md = (st + en) / 2;
left = new node();
right = new node();
left->build(st, md);
right->build(md + 1, en);
}
}
int query(int lf, int rg) {
if (st > rg || en < lf || lf > rg) return 0ll;
if (lf <= st && en <= rg) return val;
if (left == NULL) return 0ll;
return max(left->query(lf, rg), right->query(lf, rg));
}
void update(int id, int x) {
if (st > id || en < id) return;
if (st == en) {
val = max(val, x);
return;
}
lazy();
left->update(id, x);
right->update(id, x);
val = max(left->val, right->val);
}
} sg, sg2;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, x;
cin >> n >> x;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
sg.build(1, inf);
vector<int> dp(n + 1);
for (int i = n; i >= 1; i--) {
dp[i] = sg.query(a[i] + 1, inf) + 1;
sg.update(a[i], dp[i]);
}
int ans = 0;
sg2.build(1, inf);
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i] + sg2.query(1, a[i] - 1 + x));
sg2.update(a[i], sg2.query(1, a[i] - 1) + 1);
}
cout << ans << '\n';
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |