#include<bits/stdc++.h>
using namespace std;
#define ll long long
#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 pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
inline void adm(ll &x){if(x>=mod)x%=mod;else if(x<0)x+=mod;}
//--------------------------------------------------------------------
const ll N = 2e5 + 10;
ll a[N];
unordered_map<ll, ll> mp;
ll BIT[N];
ll n;
ll pre[N], suf[N];
void update(ll x, ll val) {
while(x <= n) {
BIT[x] = max(BIT[x], val);
x += x & -x;
}
}
ll get(ll x) {
ll res = 0;
while(x > 0) {
res = max(BIT[x], res);
x -= x & -x;
}
return res;
}
void update2(ll x, ll val) {
while(x > 0) {
BIT[x] = max(BIT[x], val);
x -= x & -x;
}
}
ll get2(ll x) {
ll res = 0;
while(x <= n) {
res = max(BIT[x], res);
x += x & -x;
}
return res;
}
void hbmt() {
ll x;
vector<ll> b;
cin >> n >> x;
FOR(i, 1, n) {
cin >> a[i];
b.push_back(a[i]);
b.push_back(a[i] + x);
b.push_back(a[i] - x);
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
ll cnt = 0, ans = 0;
for(auto e : b) {
mp[e] = ++cnt;
}
FOR(i, 1, n) {
ll val = get(mp[a[i]] - 1) + 1;
pre[i] = val;
ans = max(val, ans);
update(mp[a[i]], val);
}
memset(BIT, 0, sizeof BIT);
FORD(i, n, 1) {
ll val = get2(mp[a[i]] + 1) + 1;
suf[i] = val;
update2(mp[a[i]], val);
}
memset(BIT, 0, sizeof BIT);
FOR(i, 1, n) {
ll val = get(mp[a[i] + x] - 1);
ans = max(val + suf[i], ans);
update(mp[a[i]], pre[i]);
}
memset(BIT, 0, sizeof BIT);
FORD(i, n, 1) {
ll val = get2(mp[a[i] - x] + 1);
ans = max(val + pre[i], ans);
update2(mp[a[i]], suf[i]);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen("hbmt.inp", "r")) {
freopen("hbmt.inp", "r", stdin);
freopen("hbmt.out", "w", stdout);
}
// t_test
hbmt();
return 0;
}
Compilation message (stderr)
glo.cpp: In function 'int main()':
glo.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen("hbmt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen("hbmt.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... |