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 FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define PB push_back
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define fi first
#define se second
#define BIT(x, i) (((x) >> (i)) & 1)
using namespace std;
/** END OF TEMPLATE **/
const int mxN = 2e5 + 5;
int n, d;
struct fenwick{
vector<int> bit;
int sz;
fenwick(int t) : sz(t), bit(t+1, 0) {}
void update(int id, int val) {
for(; id <= sz; id += id&-id)
bit[id] = max(bit[id], val);
}
int get(int id) {
int res = 0;
for(; id > 0; id -= id&-id)
res = max(res, bit[id]);
return res;
}
};
int a[mxN], b[mxN];
int main()
{
FastIO;
//freopen("test.cpp", "r", stdin);
//freopen(".out", "w", stdout);
cin >> n >> d;
vector<int> t;
FOR(i, 1, n) {
cin >> a[i];
b[i] = a[i] + d;
t.PB(a[i]); t.PB(b[i]);
}
sort(ALL(t)); t.erase(unique(ALL(t)), t.end());
FOR(i, 1, n) {
a[i] = lower_bound(ALL(t), a[i]) - t.begin() + 1;
b[i] = lower_bound(ALL(t), b[i]) - t.begin() + 1;
}
fenwick T1(t.size()+1);
fenwick T2(t.size()+2);
int res = 0;
FOR(i, 1, n) {
int r1 = T1.get(a[i]-1) + 1;
int r2 = max(T1.get(b[i]-1), T2.get(a[i]-1)) + 1;
T1.update(a[i], r1); T2.update(a[i], r2);
res = max(r1, r2);
}
cout << res;
return 0;
}
/*
*/
Compilation message (stderr)
glo.cpp: In constructor 'fenwick::fenwick(int)':
glo.cpp:19:9: warning: 'fenwick::sz' will be initialized after [-Wreorder]
19 | int sz;
| ^~
glo.cpp:18:17: warning: 'std::vector<int> fenwick::bit' [-Wreorder]
18 | vector<int> bit;
| ^~~
glo.cpp:20:5: warning: when initialized here [-Wreorder]
20 | fenwick(int t) : sz(t), bit(t+1, 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... |