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 lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define md ((l + r) >> 1)
int seg[4'000'005], L[4'000'005], R[4'000'005], cur = 3;
int newnode()
{
return cur++;
}
void upd(int i, int l, int r, int x, int v)
{
if (x == l && r == x) return void (seg[i] = v);
if (r < x || x < l) return ;
if (x <= md)
{
if (!L[i]) L[i] = newnode();
upd(L[i], l, md, x, v);
}
else
{
if (!R[i]) R[i] = newnode();
upd(R[i], md + 1, r, x, v);
}
seg[i] = max(seg[L[i]], seg[R[i]]);
}
int get(int i, int l, int r, int s, int e)
{
if (r < s || e < l || i == 0) return 0;
if (s <= l && r <= e) return seg[i];
return max(get(L[i], l, md, s, e), get(R[i], md + 1, r, s, e));
}
signed main()
{
lesgooo;
int n, x;
cin >> n >> x;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int dp[n][2]{};
for (int i = n-1; i >= 0; i--) for (int j = 0; j < 2; j++)
{
// for (int k = i + 1; k < n; k++) if (a[k] >= a[i]+1) dp[i][j] = max(dp[i][j], dp[k][j]);
// if (!j) for (int k = i + 1; k < n; k++) if (a[i]+1-x <= a[k] && a[k] <= a[i]+1+x) dp[i][j] = max(dp[i][j], dp[k][1]);
dp[i][j] = get(j+1, 0, 1e9, a[i]+1, 1e9);
if (!j) dp[i][j] = max(dp[i][j], get(2, 0, 1e9, a[i]+1-x, a[i]+1+x));
dp[i][j]++;
upd(j+1, 0, 1e9, a[i], dp[i][j]);
}
int mx = 0;
for (int i = 0; i < n; i++) mx = max(mx, dp[i][0]);
cout << mx;
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... |