This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;
int n, x, a[N], b[N], t[8 * N], dp[N];
void update(int id, int l, int r, int p, int val) {
if(l > p || r < p) return;
if(l == r) {
t[id] = val;
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, p, val);
update(id << 1 | 1, mid + 1, r, p, val);
t[id] = max(t[id << 1], t[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v) {
if(l > v || r < u) return 0;
if(u <= l && r <= v) return t[id];
int mid = (l + r) >> 1;
int trai = get(id << 1, l, mid, u, v);
int phai = get(id << 1 | 1, mid + 1, r, u, v);
return max(trai, phai);
}
void to_nho_cau() {
cin >> n >> x;
vector<int> nen;
forr(i, 1, n) {
cin >> a[i];
b[i] = a[i] + x;
nen.pb(a[i]);
nen.pb(b[i]);
}
sort(all(nen));
int mx = 0;
nen.resize(unique(all(nen)) - nen.begin());
forr(i, 1, n) {
a[i] = lower_bound(all(nen), a[i]) - nen.begin() + 1;
b[i] = lower_bound(all(nen), b[i]) - nen.begin() + 1;
mx = max(mx, b[i]);
}
forr(i, 1, n) {
int val = get(1, 1, mx, 1, b[i] - 1) + 1;
int val2 = get(1, 1, mx, 1, a[i] - 1) + 1;
update(1, 1, mx, a[i], val2);
dp[i] = val;
}
int ans = 0;
forr(i, 1, 8 * n) t[i] = 0;
ford(i, n, 1) {
int val = get(1, 1, mx, a[i] + 1, mx) + 1;
update(1, 1, mx, a[i], val);
// cout << dp[i] << " " << val - 1 << "\n";
ans = max(ans, dp[i] + val - 1);
}
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
freopen(file".inp","r",stdin);
freopen(file".out","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--) to_nho_cau();
}
/*
1.self check:
2.long long
3.size of array
4.code for testing
5.initializing
6.modulo number
*/
/**
∧__∧
(`•ω• )づ__∧
(つ /( •ω•。)
しーJ (nnノ) pat pat
**/
/** /\_/\
* (= ._.)
* / >☕ \>💻
**/
Compilation message (stderr)
glo.cpp:109:9: warning: "/*" within comment [-Wcomment]
109 | /** /\_/\
|
# | 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... |