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 fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define all(x) (x).begin() , (x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file "name"
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
const int inf = 1e9;
const ll linf = 1e17;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int n, d, nt, res;
int a[N];
int x[N];
int st[4 * N];
int pre[N];
map <int, int> m;
void inp()
{
cin >> n >> d;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
x[i] = a[i];
}
}
struct segtree {
int st[4 * N];
segtree() {}
void update(int id, int l, int r, int i, int v) {
if(i < l || r < i) return ;
if(l == r) return void(st[id] = v);
int m = (l + r) >> 1;
update(id << 1, l, m, i, v);
update(id << 1 | 1, m + 1, r, i, v);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v) {
if(v < l || r < u) return 0;
if(l >= u && r <= v) return st[id];
int m = (l + r) >> 1;
return max(get(id << 1, l, m, u, v), get(id << 1 | 1, m + 1, r, u, v));
}
} st1, st2;
void solve()
{
sort(x + 1, x + n + 1);
nt = unique(x + 1, x + n + 1) - x - 1;
for(int i = 1; i <= n; ++i) {
int id = lower_bound(x + 1, x + nt + 1, a[i]) - x;
int cnt = st1.get(1, 1, nt, 1, id - 1) + 1;
st1.update(1, 1, nt, id, cnt);
pre[i] = m[a[i]];
m[a[i]] = cnt;
maximize(res, cnt);
}
for(int i = n; i >= 1; --i) {
int id = lower_bound(x + 1, x + nt + 1, a[i]) - x;
int cnt = st2.get(1, 1, nt, id + 1, nt) + 1;
st2.update(1, 1, nt, id, cnt);
st1.update(1, 1, nt, id, pre[i]);
id = lower_bound(x + 1, x + nt + 1, a[i] + d) - x - 1;
cnt += st1.get(1, 1, nt, 1, id);
maximize(res, cnt);
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp" , "r" , stdin);
// freopen(file".out" , "w" , stdout);
inp();
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |