#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define filter(v) v.resize(unique(all(v)) - v.begin())
#define dbg(x) "[" #x << " = " << x << "]"
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> bool maximize(T &a, T b) {
if(a < b) return a = b, true;
else return false;
}
template<typename T> bool minimize(T &a, T b) {
if(a > b) return a = b, true;
else return false;
}
template<typename T1, typename T2> T2 rnd(T1 l, T2 r) {
return uniform_int_distribution<T2>(l, r)(rng);
}
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef pair<long long, long long> pll;
const int N = 2e5 + 3;
int n, d;
int a[N];
namespace subtask_2 {
bool approve() {
return d == 0;
}
void solve() {
multiset<int> st;
for(int i = 1; i <= n; i++) {
auto it = st.lower_bound(a[i]);
if(it != st.end()) st.erase(it);
st.insert(a[i]);
}
cout << sz(st) << endl;
}
}
namespace subtask_3 {
bool approve() {
return true;
}
template<const int N> struct SegmentTree {
int n;
int st[N << 2];
void init(int _n) {n = _n;}
void reload() {
for(int i = 1; i <= (n << 2); i++) st[i] = 0;
}
void update(int id, int l, int r, int p, int v) {
if(l == r) {
maximize(st[id], v);
return void();
}
int mid = (l + r) >> 1;
if(p <= mid) update(id << 1, l, mid, p, v);
else update(id << 1 | 1, mid + 1, r, p, v);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v) {
if(u > v || r < u || v < l) return 0;
if(u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
};
int prefix[N], suffix[N];
SegmentTree<N> st;
vector<int> cps;
int getID(int id) {
return lower_bound(all(cps), id) - cps.begin() + 1;
}
void solve() {
vector<int> lis;
for(int i = 1; i <= n; i++) {
int pos = lower_bound(all(lis), a[i]) - lis.begin();
prefix[i] = pos + 1;
if(pos == sz(lis)) lis.push_back(a[i]);
else lis[pos] = a[i];
}
lis.clear();
for(int i = n; i >= 1; i--) {
int pos = lower_bound(all(lis), -a[i]) - lis.begin();
suffix[i] = pos + 1;
if(pos == sz(lis)) lis.push_back(-a[i]);
else lis[pos] = -a[i];
}
for(int i = 1; i <= n; i++) {
cps.push_back(a[i]);
cps.push_back(a[i] - d);
cps.push_back(a[i] + d);
}
sort(all(cps)); filter(cps);
int m = sz(cps), answer = 0;
st.init(n);
for(int i = 1; i <= n; i++) {
maximize(answer, suffix[i] + st.get(1, 1, m, 1, getID(a[i] + d) - 1));
st.update(1, 1, m, getID(a[i]), prefix[i]);
}
st.reload();
for(int i = n; i >= 1; i--) {
maximize(answer, prefix[i] + st.get(1, 1, m, getID(a[i] + d) + 1, m));
st.update(1, 1, m, getID(a[i]), suffix[i]);
}
cout << answer << endl;
// for(int i = 1; i <= n; i++) {
// cout << prefix[i] << " \n"[i == n];
// }
//
// for(int i = 1; i <= n; i++) {
// cout << suffix[i] << " \n"[i == n];
// }
}
}
void process() {
if(subtask_2::approve()) return subtask_2::solve();
if(subtask_3::approve()) return subtask_3::solve();
}
void input() {
cin >> n >> d;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "DAYDEP"
if(fopen(task".INP", "r")) {
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int testcase = 1; // cin >> testcase;
for(int i = 1; i <= testcase; i++) {
input();
process();
}
return 0;
}
Compilation message (stderr)
glo.cpp: In function 'int main()':
glo.cpp:166:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
166 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:167:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |