#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 2e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
void add(int &x, int y) {x += y; if (x >= mod) x -= mod;}
void sub(int &x, int y) {x -= y; if (x < 0) x += mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
// -----------------------------------------//
struct Segtree {
int n;
vector<int> st;
Segtree(int _n = 0) {
n = _n;
st.assign(4 * n + 5, 0);
}
void update(int id, int l, int r, int p, int c) {
if (l == r) {
st[id] = max(st[id], c);
return;
}
int mid = (l + r) >> 1;
if (p <= mid) update(id<<1, l, mid, p, c);
else update(id<<1|1, mid + 1, r, p, c);
st[id] = max(st[id<<1], st[id<<1|1]);
}
int getMax(int id, int l, int r, int u, int v) {
if (u > r || v < l || l > r || u > v) return 0;
if (u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return max(getMax(id<<1, l, mid, u, v), getMax(id<<1|1, mid + 1, r, u, v));
}
void update(int p, int c) {
return update(1, 1, n, p, c);
}
int getMax(int l, int r) {
return getMax(1, 1, n, l, r);
}
} it;
int n, X;
int a[N], b[N];
vector<int> val;
void read() {
cin >> n >> X;
for(int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
}
void compress() {
for(int i = 1; i <= n; i++) {
val.push_back(a[i]);
val.push_back(a[i] - 1);
val.push_back(a[i] + 1);
val.push_back(a[i] - 1 + X);
}
sort(ALL(val));
val.erase(unique(ALL(val)), val.end());
for(int i = 1; i <= n; i++) {
a[i] = lower_bound(ALL(val), a[i]) - val.begin() + 1;
}
}
int f[N];
void solve() {
compress();
// for(int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl;
it = Segtree(SZ(val));
for(int i = 1; i <= n; i++) {
int l = lower_bound(ALL(val), b[i] - 1) - val.begin() + 1;
int r = lower_bound(ALL(val), b[i] + X - 1) - val.begin() + 1;
f[i] = it.getMax(l, r);
int res = it.getMax(1, a[i] - 1);
it.update(a[i], res + 1);
}
int ans = 1;
it = Segtree(SZ(val));
for(int i = n; i >= 1; i--) {
int res = it.getMax(a[i] + 1, SZ(val));
ans = max(ans, res + 1 + f[i]);
it.update(a[i], res + 1);
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
glo.cpp: In function 'int main()':
glo.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | 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... |