#include <bits/stdc++.h>
using namespace std;
#define el "\n"
#define FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lg(x) __lg(x)
#define alla(a,n) a+1,a+n+1
#define ll long long
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
const int N = 2e5 + 2;
const int INF = 1e9 + 2;
int n, a[N], lim;
void inp()
{
cin >> n >> lim;
FOR(i, 1, n) cin >> a[i];
}
/* Try your best
No regrets */
namespace subtask_3
{
int temp[N];
void slv()
{
int ans = 0;
for(int i = 1; i <= n; i++) {
a[i] -= lim;
for(int j = 1; j <= n; j++) temp[j] = INF;
for(int j = 1; j <= n; j++) {
int pos = lower_bound(temp + 1, temp + n + 1, a[j]) - temp;
maxi(ans, pos);
temp[pos] = a[j];
}
}
cout << ans;
}
}
namespace subtask_4
{
int temp[N], dp[N];
void slv()
{
FOR(i, 1, n) temp[i] = INF;
int ans = 0;
FOR(i, 1, n) {
int pos = lower_bound(temp + 1, temp + n + 1, a[i]) - temp;
dp[i] = pos;
temp[pos] = a[i];
maxi(ans, pos);
}
cout << ans;
}
}
namespace subtask_7
{
int Left[N], Right[N];
int res = 0;
int temp[N];
void prepare()
{
FOR(i, 1, n) temp[i] = INF;
int ans = 0;
for(int i = 1; i <= n; i++) {
int pos = lower_bound(temp + 1, temp + n + 1, a[i] - lim) - temp;
temp[pos] = a[i];
maxi(ans, pos);
Left[i] = ans;
}
FOR(i, 1, n) temp[i] = INF;
ans = 0;
for(int i = n; i >= 1; i--) {
int pos = lower_bound(temp + 1, temp + n + 1, - a[i]) - temp;
temp[pos] = - a[i];
maxi(ans, pos);
Right[i] = ans;
}
}
void slv()
{
prepare();
int ans = 0;
FOR(i, 1, n) {
maxi(ans, Left[i] + Right[i] - 1);
}
cout << ans;
}
}
/* Code slowly, think carefully */
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define __Azul__ "glo"
if(fopen(__Azul__".inp", "r")) {
freopen(__Azul__".inp", "r", stdin);
freopen(__Azul__".out", "w", stdout);
}
bool qs = 0;
int T = 1;
if(qs) cin >> T;
while(T--) {
inp();
subtask_7::slv();
// if(!lim) subtask_4::slv();
// else if(n <= 1000) subtask_3::slv();
// else subtask_7::slv();
}
cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
Compilation message (stderr)
glo.cpp:114:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
114 | main()
| ^~~~
glo.cpp: In function 'int main()':
glo.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen(__Azul__".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(__Azul__".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... |